ave / ave-button.js
sylvance's picture
Upload 33 files
dfb04cf verified
Raw History Blame Contribute Delete
5.51 kB
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);