Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e5b307df1b | ||
|
|
b201f6be88 |
4 changed files with 81 additions and 62 deletions
|
|
@ -5,18 +5,15 @@
|
||||||
// used).
|
// used).
|
||||||
//
|
//
|
||||||
// `el(tag, attrs, ...children)` creates an element, applying `attrs` as
|
// `el(tag, attrs, ...children)` creates an element, applying `attrs` as
|
||||||
// either the `class`/`html`/`is` special cases or plain attributes, and
|
// either the `class`/`html` special cases or plain attributes, and
|
||||||
// appending `children` (strings become text nodes, `null`/`undefined`
|
// appending `children` (strings become text nodes, `null`/`undefined`
|
||||||
// entries are skipped so callers can inline conditional children).
|
// entries are skipped so callers can inline conditional children).
|
||||||
// `is: 'custom-name'` creates a customized built-in element (e.g.
|
// `tag` can be any custom element's tag name too (e.g. `el('hive-btn',
|
||||||
// `el('button', { is: 'hive-btn' }, 'label')` → `<button is="hive-btn">`)
|
// { variant: 'danger' }, 'label')`) — `document.createElement` upgrades
|
||||||
// — passed to `document.createElement` itself, since a customized
|
// it automatically if the tag is already registered.
|
||||||
// built-in has to be created with its `is` option up front, not upgraded
|
|
||||||
// after the fact via `setAttribute`.
|
|
||||||
export const el = (tag, attrs = {}, ...children) => {
|
export const el = (tag, attrs = {}, ...children) => {
|
||||||
const e = attrs.is ? document.createElement(tag, { is: attrs.is }) : document.createElement(tag);
|
const e = document.createElement(tag);
|
||||||
for (const [k, v] of Object.entries(attrs)) {
|
for (const [k, v] of Object.entries(attrs)) {
|
||||||
if (k === 'is') continue;
|
|
||||||
if (k === 'class') e.className = v;
|
if (k === 'class') e.className = v;
|
||||||
else if (k === 'html') e.innerHTML = v;
|
else if (k === 'html') e.innerHTML = v;
|
||||||
else e.setAttribute(k, v);
|
else e.setAttribute(k, v);
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,24 @@
|
||||||
/* hive-btn.css — scoped stylesheet for the <hive-btn> customized built-in
|
/* hive-btn.css — shadow-DOM stylesheet for the <hive-btn> autonomous
|
||||||
button element (hive-btn.js). Loaded as raw text at build time
|
custom element (hive-btn.js). Loaded as raw text at build time
|
||||||
(esbuild's `text` loader) and turned into a `CSSStyleSheet` adopted by
|
(esbuild's `text` loader) and adopted into each instance's own shadow
|
||||||
the element's own shadow root. `:host` styles the button itself (the
|
root. `:host` is an invisible wrapper (`display: contents`) — the
|
||||||
element *is* a real `<button is="hive-btn">`, so `:host(:hover)` /
|
real box is the plain `button` selector below, targeting the actual
|
||||||
`:host(:disabled)` select genuine native pseudo-classes, not
|
`<button>` the shadow root wraps (scope-isolated by the shadow
|
||||||
hand-rolled state tracking) — this is the base look every `<hive-btn>`
|
boundary already, no clash risk with anything outside). `variant` is
|
||||||
gets; `variant` is an attribute (not a class) since it's a semantic
|
an attribute on the HOST (how callers set it, and where hive-dialog's
|
||||||
property of the component, not an arbitrary styling hook. */
|
own selectors read it back); `:host([variant="…"])` sets `color`,
|
||||||
|
which then inherits down into the shadow tree the normal way. */
|
||||||
|
|
||||||
:host {
|
:host {
|
||||||
|
display: contents;
|
||||||
|
}
|
||||||
|
:host([variant="cancel"]) { color: var(--subtext0); }
|
||||||
|
:host([variant="confirm"]) { color: var(--green); }
|
||||||
|
:host([variant="danger"]) { color: var(--red); }
|
||||||
|
|
||||||
|
button {
|
||||||
font-family: inherit;
|
font-family: inherit;
|
||||||
|
font-size: inherit;
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
text-transform: uppercase;
|
text-transform: uppercase;
|
||||||
letter-spacing: 0.1em;
|
letter-spacing: 0.1em;
|
||||||
|
|
@ -24,17 +33,14 @@
|
||||||
box-shadow: 0 0 0 0 currentColor;
|
box-shadow: 0 0 0 0 currentColor;
|
||||||
transition: box-shadow 0.15s ease;
|
transition: box-shadow 0.15s ease;
|
||||||
}
|
}
|
||||||
:host(:hover) {
|
button:hover {
|
||||||
background: color-mix(in srgb, var(--fg) 6%, transparent);
|
background: color-mix(in srgb, var(--fg) 6%, transparent);
|
||||||
text-shadow: 0 0 10px currentColor;
|
text-shadow: 0 0 10px currentColor;
|
||||||
box-shadow: 0 0 10px -2px currentColor;
|
box-shadow: 0 0 10px -2px currentColor;
|
||||||
}
|
}
|
||||||
:host(:disabled) {
|
button:disabled {
|
||||||
opacity: 0.32;
|
opacity: 0.32;
|
||||||
cursor: not-allowed;
|
cursor: not-allowed;
|
||||||
text-shadow: none;
|
text-shadow: none;
|
||||||
box-shadow: none;
|
box-shadow: none;
|
||||||
}
|
}
|
||||||
:host([variant="cancel"]) { color: var(--subtext0); }
|
|
||||||
:host([variant="confirm"]) { color: var(--green); }
|
|
||||||
:host([variant="danger"]) { color: var(--red); }
|
|
||||||
|
|
|
||||||
|
|
@ -1,40 +1,58 @@
|
||||||
// hive-btn.js — <hive-btn>, a customized built-in `<button>` (`<button
|
// hive-btn.js — <hive-btn>, the button primitive for modal.js's dialog
|
||||||
// is="hive-btn">`) with a shadow root for style encapsulation, replacing
|
// buttons. Autonomous custom element (previously a customized built-in,
|
||||||
// the light-DOM `.btn` class as the button primitive for shadow-DOM
|
// `<button is="hive-btn">` — dropped: `is=` upgrades can't host a shadow
|
||||||
// consumers (starting with modal.js's dialog buttons — see the frontend
|
// root at all, which crashed every themed dialog, and reads as a hack
|
||||||
// components-split discussion on the forge issue tracker).
|
// regardless once you know that). A real `<button>` lives inside the
|
||||||
|
// shadow root instead, so native click/keyboard activation and
|
||||||
|
// `:disabled` still come for free — just one level down from the host.
|
||||||
//
|
//
|
||||||
// Extending `HTMLButtonElement` (a "customized built-in element", not an
|
// `delegatesFocus: true` on the shadow root means `.focus()` on the host
|
||||||
// autonomous one) keeps every native `<button>` behaviour for free —
|
// (what modal.js calls) focuses the inner button directly, and a click
|
||||||
// click/keyboard activation, `:disabled`, form participation/submission —
|
// anywhere on the host focuses it too, matching native `<button>` feel.
|
||||||
// instead of re-implementing them on a generic wrapper. The shadow root
|
// The inner button's native `click` is a composed event, so it bubbles
|
||||||
// holds only a `<style>`-equivalent (adopted stylesheet) plus a `<slot>`
|
// out through the shadow boundary — callers add `click` listeners on the
|
||||||
// so the button's light-DOM content (its label, or asyncBtn's swapped-in
|
// `<hive-btn>` host exactly as they would on a plain `<button>`.
|
||||||
// spinner span) renders through unchanged — slotted content stays styled
|
|
||||||
// by the light-DOM cascade (global `.spinner` etc. still apply), only the
|
|
||||||
// host element's own box/text styling is shadow-scoped.
|
|
||||||
//
|
//
|
||||||
// Customized built-ins aren't supported in Safari/WebKit (a deliberate
|
// Usage: `el('hive-btn', { type: 'button', variant: 'danger' }, 'label')`.
|
||||||
// WebKit-team stance, unlikely to change) — fine here since the project
|
// `variant` ∈ 'cancel' | 'confirm' | 'danger' | unset (neutral default) —
|
||||||
// targets recent Firefox only.
|
// reflected straight through to the inner button so hive-btn.css's
|
||||||
//
|
// `:host([variant="…"])` selectors keep working unchanged.
|
||||||
// Usage: `el('button', { type: 'button', is: 'hive-btn', variant: 'danger' }, 'label')`
|
// `disabled`/`type` are likewise plain attributes on the host, mirrored
|
||||||
// (dom.js's `el()` passes `is` to `document.createElement` so custom-
|
// onto the inner button on connect and on every attribute change.
|
||||||
// element upgrade happens at creation, not after). `variant` is one of
|
|
||||||
// 'cancel' | 'confirm' | 'danger' | (unset, for the neutral/default look)
|
|
||||||
// — a plain attribute, not a class, since it's a semantic prop of the
|
|
||||||
// component rather than an arbitrary styling hook.
|
|
||||||
|
|
||||||
import hiveBtnCss from './hive-btn.css';
|
import hiveBtnCss from './hive-btn.css';
|
||||||
|
|
||||||
class HiveBtn extends HTMLButtonElement {
|
const OBSERVED = ['disabled', 'type'];
|
||||||
|
|
||||||
|
class HiveBtn extends HTMLElement {
|
||||||
|
static get observedAttributes() {
|
||||||
|
return OBSERVED;
|
||||||
|
}
|
||||||
|
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
if (this.shadowRoot) return; // guard: connectedCallback can re-fire (e.g. re-parenting)
|
if (this._btn) {
|
||||||
const root = this.attachShadow({ mode: 'open' });
|
this._sync();
|
||||||
|
return; // already built (e.g. re-parenting re-fires connectedCallback)
|
||||||
|
}
|
||||||
|
const root = this.attachShadow({ mode: 'open', delegatesFocus: true });
|
||||||
const sheet = new CSSStyleSheet();
|
const sheet = new CSSStyleSheet();
|
||||||
sheet.replaceSync(hiveBtnCss);
|
sheet.replaceSync(hiveBtnCss);
|
||||||
root.adoptedStyleSheets = [sheet];
|
root.adoptedStyleSheets = [sheet];
|
||||||
root.append(document.createElement('slot'));
|
const btn = document.createElement('button');
|
||||||
|
btn.append(document.createElement('slot'));
|
||||||
|
root.append(btn);
|
||||||
|
this._btn = btn;
|
||||||
|
this._sync();
|
||||||
|
}
|
||||||
|
|
||||||
|
attributeChangedCallback() {
|
||||||
|
this._sync();
|
||||||
|
}
|
||||||
|
|
||||||
|
_sync() {
|
||||||
|
if (!this._btn) return;
|
||||||
|
this._btn.disabled = this.hasAttribute('disabled');
|
||||||
|
this._btn.type = this.getAttribute('type') || 'button';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
customElements.define('hive-btn', HiveBtn, { extends: 'button' });
|
customElements.define('hive-btn', HiveBtn);
|
||||||
|
|
|
||||||
|
|
@ -4,15 +4,13 @@
|
||||||
// actions and prompts match each page's chrome instead of a jarring OS
|
// actions and prompts match each page's chrome instead of a jarring OS
|
||||||
// dialog.
|
// dialog.
|
||||||
//
|
//
|
||||||
// Implemented as two shadow-DOM custom elements (`<hive-dialog>`,
|
// Implemented as three shadow-DOM custom elements (`<hive-dialog>`,
|
||||||
// `<hive-toast>`), plus `<hive-btn>` (hive-btn.js) for the dialog's own
|
// `<hive-toast>`, and `<hive-btn>` for the dialog's own buttons) — real
|
||||||
// buttons — real per-component style encapsulation, CSS in real `.css`
|
// per-component style encapsulation, CSS in real `.css` files imported
|
||||||
// files imported as raw text (see each component's own header comment
|
// as raw text (see each component's own header comment for the design
|
||||||
// for the design rationale: shadow-DOM-vs-light-DOM tradeoffs live atop
|
// rationale). Other `.btn` consumers across the app stay on the
|
||||||
// the `<hive-dialog>`/`<hive-toast>` classes below, the customized-
|
// light-DOM `.btn` class for now — migrating them is a separate
|
||||||
// built-in choice lives in hive-btn.js). Other `.btn` consumers across
|
// follow-up.
|
||||||
// the app stay on the light-DOM `.btn` class for now — migrating them
|
|
||||||
// is a separate follow-up.
|
|
||||||
//
|
//
|
||||||
// `openDialog` is the general primitive (any title/message/content + a row of
|
// `openDialog` is the general primitive (any title/message/content + a row of
|
||||||
// buttons); `themedConfirm` is a thin cancel/confirm wrapper with optional
|
// buttons); `themedConfirm` is a thin cancel/confirm wrapper with optional
|
||||||
|
|
@ -78,8 +76,8 @@ class HiveDialog extends HTMLElement {
|
||||||
// overrides it to the 'danger' look regardless (a destructive
|
// overrides it to the 'danger' look regardless (a destructive
|
||||||
// confirm button reads as danger, not as a plain confirm).
|
// confirm button reads as danger, not as a plain confirm).
|
||||||
const variant = b.danger ? 'danger' : b.class;
|
const variant = b.danger ? 'danger' : b.class;
|
||||||
const btn = el('button', {
|
const btn = el('hive-btn', {
|
||||||
type: 'button', is: 'hive-btn',
|
type: 'button',
|
||||||
...(variant ? { variant } : {}),
|
...(variant ? { variant } : {}),
|
||||||
}, b.label);
|
}, b.label);
|
||||||
btn.addEventListener('click', () => done(b.value));
|
btn.addEventListener('click', () => done(b.value));
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue