File size: 5,510 Bytes
dfb04cf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | import { BaseComponent } from "./ave-base-component.js";
class AveButton extends BaseComponent {
static get observedAttributes() {
return ['variant', 'size', 'disabled', 'loading', 'icon-only', 'type'];
}
constructor() {
super();
this._handleClick = this._handleClick.bind(this);
}
// Property getters/setters
get variant() { return this.getAttribute('variant') || 'primary'; }
set variant(value) { this.setAttribute('variant', value); }
get size() { return this.getAttribute('size') || 'default'; }
set size(value) { this.setAttribute('size', value); }
get disabled() { return this._getBooleanAttribute('disabled'); }
set disabled(value) { this._setBooleanAttribute('disabled', value); }
get loading() { return this._getBooleanAttribute('loading'); }
set loading(value) { this._setBooleanAttribute('loading', value); }
get iconOnly() { return this._getBooleanAttribute('icon-only'); }
set iconOnly(value) { this._setBooleanAttribute('icon-only', value); }
get type() { return this.getAttribute('type') || 'button'; }
set type(value) { this.setAttribute('type', value); }
setupEventListeners() {
this._registerEvent('button', 'click', this._handleClick);
}
_handleClick(e) {
if (this.disabled || this.loading) {
e.preventDefault();
e.stopPropagation();
return;
}
this._dispatch('ave-click', {
variant: this.variant,
size: this.size,
disabled: this.disabled,
loading: this.loading
});
}
_getButtonClasses() {
const baseClasses = [
'inline-flex',
'items-center',
'justify-center',
'whitespace-nowrap',
'rounded-md',
'text-sm',
'font-medium',
'ring-offset-background',
'transition-colors',
'focus-visible:outline-none',
'focus-visible:ring-2',
'focus-visible:ring-ring',
'focus-visible:ring-offset-2',
'disabled:pointer-events-none',
'disabled:opacity-50'
];
// Size classes with improved spacing
const sizeClasses = {
'sm': ['h-9', 'rounded-md', 'px-3', 'text-xs'],
'default': ['h-10', 'px-4', 'py-2'],
'lg': ['h-11', 'rounded-md', 'px-8'],
'icon': ['h-10', 'w-10']
};
// Enhanced variant classes with better color handling
const variantClasses = {
'primary': [
'bg-primary',
'text-primary-foreground',
'hover:bg-primary/90',
'shadow'
],
'secondary': [
'bg-secondary',
'text-secondary-foreground',
'hover:bg-secondary/80',
'shadow-sm'
],
'destructive': [
'bg-destructive',
'text-destructive-foreground',
'hover:bg-destructive/90',
'shadow-sm'
],
'outline': [
'border',
'border-input',
'bg-background',
'hover:bg-accent',
'hover:text-accent-foreground'
],
'ghost': [
'hover:bg-accent',
'hover:text-accent-foreground'
],
'link': [
'text-primary',
'underline-offset-4',
'hover:underline'
]
};
const size = this.iconOnly ? 'icon' : (this.size || 'default');
const variant = this.variant || 'primary';
const selectedSizeClasses = sizeClasses[size] || sizeClasses['default'];
const selectedVariantClasses = variantClasses[variant] || variantClasses['primary'];
return [
...baseClasses,
...selectedSizeClasses,
...selectedVariantClasses
].join(' ');
}
render() {
const buttonClasses = this._getButtonClasses();
const content = this.innerHTML || 'Button';
this.shadowRoot.innerHTML = `
<style>
:host {
display: inline-block;
}
.loading-spinner {
animation: spin 1s linear infinite;
width: 1rem;
height: 1rem;
margin-right: 0.5rem;
}
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
.underline-offset-4 {
text-decoration-line: underline;
text-underline-offset: 4px;
}
.hover\\:underline:hover {
text-decoration-line: underline;
}
/* Enhanced focus styles */
button:focus-visible {
outline: 2px solid var(--ring);
outline-offset: 2px;
}
/* Improved disabled state */
button:disabled {
cursor: not-allowed;
}
/* Better loading state */
button[data-loading="true"] {
cursor: wait;
}
/* Icon-only button adjustments */
button[data-icon-only="true"] {
padding: 0;
}
</style>
<button
class="${buttonClasses}"
type="${this.type}"
${this.disabled ? 'disabled' : ''}
${this.loading ? 'disabled data-loading="true"' : ''}
${this.iconOnly ? 'data-icon-only="true"' : ''}
aria-label="${this.getAttribute('aria-label') || ''}"
>
${this.loading ? `
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="loading-spinner">
<path d="M21 12a9 9 0 11-6.219-8.56"/>
</svg>
` : ''}
<slot>${content}</slot>
</button>
`;
this.setupEventListeners();
}
}
customElements.define('ave-button', AveButton);
|