|
| 1 | +/** |
| 2 | + * A custom web component that renders a button with customizable styles. |
| 3 | + * |
| 4 | + * @example |
| 5 | + * ```html |
| 6 | + * <custom-button type="primary" size="large">Click me</custom-button> |
| 7 | + * ``` |
| 8 | + */ |
| 9 | +/** |
| 10 | + * A demo of a custom button component with different styles and sizes. |
| 11 | + * |
| 12 | + * @demo |
| 13 | + * ```html |
| 14 | + * <custom-button type="primary" size="large">Primary Large</custom-button> |
| 15 | + * <custom-button type="secondary" size="medium">Secondary Medium</custom-button> |
| 16 | + * <custom-button type="ghost" size="small">Ghost Small</custom-button> |
| 17 | + * ``` |
| 18 | + */ |
| 19 | +class CustomButton extends HTMLElement { |
| 20 | + /** The button type: 'primary', 'secondary', or 'ghost'. Default is 'primary'. */ |
| 21 | + private _type: string = 'primary'; |
| 22 | + /** The button size: 'small', 'medium', or 'large'. Default is 'medium'. */ |
| 23 | + private _size: string = 'medium'; |
| 24 | + |
| 25 | + constructor() { |
| 26 | + super(); |
| 27 | + this.attachShadow({ mode: 'open' }); |
| 28 | + this.render(); |
| 29 | + } |
| 30 | + |
| 31 | + static get observedAttributes() { |
| 32 | + return ['type', 'size']; |
| 33 | + } |
| 34 | + |
| 35 | + attributeChangedCallback(name: string, oldValue: string, newValue: string) { |
| 36 | + if (oldValue !== newValue) { |
| 37 | + if (name === 'type') this._type = newValue; |
| 38 | + if (name === 'size') this._size = newValue; |
| 39 | + this.render(); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + private render() { |
| 44 | + const style = document.createElement('style'); |
| 45 | + style.textContent = ` |
| 46 | + :host { |
| 47 | + display: inline-block; |
| 48 | + } |
| 49 | + button { |
| 50 | + font-family: system-ui; |
| 51 | + border-radius: 4px; |
| 52 | + border: none; |
| 53 | + cursor: pointer; |
| 54 | + transition: all 0.2s; |
| 55 | + } |
| 56 | + button.primary { |
| 57 | + background: #0066cc; |
| 58 | + color: white; |
| 59 | + } |
| 60 | + button.secondary { |
| 61 | + background: #e0e0e0; |
| 62 | + color: #333; |
| 63 | + } |
| 64 | + button.ghost { |
| 65 | + background: transparent; |
| 66 | + border: 1px solid #0066cc; |
| 67 | + color: #0066cc; |
| 68 | + } |
| 69 | + button.small { |
| 70 | + padding: 4px 8px; |
| 71 | + font-size: 12px; |
| 72 | + } |
| 73 | + button.medium { |
| 74 | + padding: 8px 16px; |
| 75 | + font-size: 14px; |
| 76 | + } |
| 77 | + button.large { |
| 78 | + padding: 12px 24px; |
| 79 | + font-size: 16px; |
| 80 | + } |
| 81 | + `; |
| 82 | + |
| 83 | + const button = document.createElement('button'); |
| 84 | + button.className = `${this._type} ${this._size}`; |
| 85 | + button.innerHTML = '<slot></slot>'; |
| 86 | + |
| 87 | + this.shadowRoot!.innerHTML = ''; |
| 88 | + this.shadowRoot!.appendChild(style); |
| 89 | + this.shadowRoot!.appendChild(button); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +customElements.define('custom-button', CustomButton); |
0 commit comments