@ktbsh/ui 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/custom-elements.json +155 -0
- package/dist/react/preset-toggle.d.ts +18 -0
- package/dist/react/preset-toggle.js +30 -0
- package/dist/react/scroll-top.d.ts +20 -0
- package/dist/react/scroll-top.js +30 -0
- package/dist/react/theme-toggle.d.ts +18 -0
- package/dist/react/theme-toggle.js +30 -0
- package/dist/react/toggle-group.d.ts +17 -0
- package/dist/react/toggle-group.js +30 -0
- package/dist/vanilla/checkbox.js +53 -9
- package/dist/vanilla/checkbox.src.js +1 -1
- package/dist/vanilla/input.js +6 -0
- package/dist/vanilla/input.src.js +1 -1
- package/dist/vanilla/preset-toggle.d.ts +2 -0
- package/dist/vanilla/preset-toggle.js +72 -0
- package/dist/vanilla/preset-toggle.src.js +271 -0
- package/dist/vanilla/radio.js +44 -10
- package/dist/vanilla/radio.src.js +1 -1
- package/dist/vanilla/scroll-top.d.ts +2 -0
- package/dist/vanilla/scroll-top.js +69 -0
- package/dist/vanilla/scroll-top.src.js +251 -0
- package/dist/vanilla/select.js +42 -3
- package/dist/vanilla/select.src.js +1 -1
- package/dist/vanilla/skeleton.js +8 -3
- package/dist/vanilla/skeleton.src.js +1 -1
- package/dist/vanilla/switch.js +57 -19
- package/dist/vanilla/switch.src.js +1 -1
- package/dist/vanilla/textarea.js +6 -0
- package/dist/vanilla/textarea.src.js +1 -1
- package/dist/vanilla/theme-toggle.d.ts +2 -0
- package/dist/vanilla/theme-toggle.js +72 -0
- package/dist/vanilla/theme-toggle.src.js +242 -0
- package/dist/vanilla/toggle-group.d.ts +2 -0
- package/dist/vanilla/toggle-group.js +19 -0
- package/dist/vanilla/toggle-group.src.js +215 -0
- package/package.json +1 -1
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
|
|
2
|
+
import { render, html } from 'uhtml';
|
|
3
|
+
|
|
4
|
+
const sheet = new CSSStyleSheet();
|
|
5
|
+
sheet.replaceSync("\n\n :host {\n display: inline-block;\n font-family: var(--kb-font-family-sans);\n }\n button {\n box-sizing: border-box;\n margin: 0;\n min-height: 2.25rem;\n padding: 0.45rem 0.8rem;\n border: 1px solid var(--kb-color-border-default);\n border-radius: var(--kb-radius-sm);\n background: var(--kb-color-bg-subtle);\n color: var(--kb-color-accent-default);\n font: inherit;\n font-size: var(--kb-font-size-sm);\n font-weight: var(--kb-font-weight-medium);\n line-height: 1;\n letter-spacing: 0.08em;\n text-transform: lowercase;\n cursor: pointer;\n transition:\n color 0.2s ease,\n background-color 0.2s ease,\n border-color 0.2s ease;\n }\n button:hover {\n color: var(--kb-color-fg-default);\n background: var(--kb-color-bg-surface);\n border-color: var(--kb-color-border-focus);\n }\n button:focus-visible {\n outline: 2px solid var(--kb-color-border-focus);\n outline-offset: 2px;\n color: var(--kb-color-fg-default);\n background: var(--kb-color-bg-surface);\n }\n /* Segmented group (set by kitbash-toggle-group) */\n :host([data-group-pos=\"start\"]) button {\n border-top-right-radius: 0;\n border-bottom-right-radius: 0;\n border-right-width: 0;\n }\n :host([data-group-pos=\"end\"]) button {\n border-top-left-radius: 0;\n border-bottom-left-radius: 0;\n }\n :host([data-group-pos=\"mid\"]) button {\n border-radius: 0;\n border-right-width: 0;\n }\n :host([data-group-pos]:hover),\n :host([data-group-pos]:focus-within) {\n position: relative;\n z-index: 1;\n }\n @media (prefers-reduced-motion: reduce) {\n button {\n transition: none;\n }\n }\n ");
|
|
6
|
+
|
|
7
|
+
export class KitbashThemeToggle extends HTMLElement {
|
|
8
|
+
|
|
9
|
+
static get observedAttributes() {
|
|
10
|
+
return ["value","storageKey"];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
static get propTypes() {
|
|
14
|
+
return {"value":"String","storageKey":"String"};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
get value() {
|
|
19
|
+
return this._props['value'];
|
|
20
|
+
}
|
|
21
|
+
set value(val) {
|
|
22
|
+
// External / framework property writes: re-render, do not emit kitbash-change
|
|
23
|
+
// (caller already knows the value they set).
|
|
24
|
+
this._assignProp('value', val);
|
|
25
|
+
this.update();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
get storageKey() {
|
|
29
|
+
return this._props['storageKey'];
|
|
30
|
+
}
|
|
31
|
+
set storageKey(val) {
|
|
32
|
+
// External / framework property writes: re-render, do not emit kitbash-change
|
|
33
|
+
// (caller already knows the value they set).
|
|
34
|
+
this._assignProp('storageKey', val);
|
|
35
|
+
this.update();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
constructor() {
|
|
40
|
+
super();
|
|
41
|
+
this.attachShadow({ mode: 'open', delegatesFocus: true });
|
|
42
|
+
|
|
43
|
+
this.shadowRoot.adoptedStyleSheets = [sheet];
|
|
44
|
+
|
|
45
|
+
this._defaults = {"value":"","storageKey":"theme"};
|
|
46
|
+
|
|
47
|
+
this._state = {};
|
|
48
|
+
this._props = { ...this._defaults };
|
|
49
|
+
this._eventHandlers = { 'click button': function(_e, { props, commit }) {
|
|
50
|
+
const next = (props.value === "light" || props.value === "dark" ? props.value : typeof document < "u" && document.documentElement.dataset.theme === "light" ? "light" : "dark") === "light" ? "dark" : "light";
|
|
51
|
+
if (typeof document < "u")
|
|
52
|
+
document.documentElement.dataset.theme = next;
|
|
53
|
+
const key = typeof props.storageKey === "string" ? props.storageKey : "theme";
|
|
54
|
+
if (key && typeof localStorage < "u")
|
|
55
|
+
try {
|
|
56
|
+
localStorage.setItem(key, next);
|
|
57
|
+
} catch {}
|
|
58
|
+
commit({ props: { value: next } });
|
|
59
|
+
} };
|
|
60
|
+
this._eventCleanup = [];
|
|
61
|
+
this._renderFn = function({ props, html }) {
|
|
62
|
+
let theme = props.value === "light" || props.value === "dark" ? props.value : "";
|
|
63
|
+
if (!theme && typeof document < "u")
|
|
64
|
+
theme = document.documentElement.dataset.theme === "light" ? "light" : "dark";
|
|
65
|
+
if (!theme)
|
|
66
|
+
theme = "dark";
|
|
67
|
+
const isLight = theme === "light";
|
|
68
|
+
return html`
|
|
69
|
+
<button
|
|
70
|
+
part="toggle-root"
|
|
71
|
+
type="button"
|
|
72
|
+
aria-pressed=${isLight ? "true" : "false"}
|
|
73
|
+
aria-label=${isLight ? "Switch to dark theme" : "Switch to light theme"}
|
|
74
|
+
>
|
|
75
|
+
${isLight ? "theme=light" : "theme=night"}
|
|
76
|
+
</button>
|
|
77
|
+
`;
|
|
78
|
+
};
|
|
79
|
+
this._reflecting = false;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
connectedCallback() {
|
|
83
|
+
// Initial attribute values are parsed by attributeChangedCallback before connectedCallback fires
|
|
84
|
+
this.update();
|
|
85
|
+
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
disconnectedCallback() {
|
|
89
|
+
this.cleanupEvents();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
attributeChangedCallback(name, oldValue, newValue) {
|
|
93
|
+
// Skip when we caused the attribute write ourselves (avoids double update)
|
|
94
|
+
if (this._reflecting) return;
|
|
95
|
+
if (oldValue === newValue) return;
|
|
96
|
+
|
|
97
|
+
const type = this.constructor.propTypes[name];
|
|
98
|
+
|
|
99
|
+
let parsedValue = newValue;
|
|
100
|
+
if (newValue === null || newValue === undefined) {
|
|
101
|
+
parsedValue = this._defaults[name];
|
|
102
|
+
} else if (type === 'Number' && newValue !== '') {
|
|
103
|
+
// Match property coercion: empty string is not Number('') → 0
|
|
104
|
+
parsedValue = Number(newValue);
|
|
105
|
+
} else if (type === 'Boolean') {
|
|
106
|
+
parsedValue = newValue !== null && newValue !== 'false';
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
this._props[name] = parsedValue;
|
|
110
|
+
|
|
111
|
+
this.update();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Coerce + reflect a single prop; no re-render (caller decides). */
|
|
115
|
+
_assignProp(key, val) {
|
|
116
|
+
const type = this.constructor.propTypes[key];
|
|
117
|
+
let next = val;
|
|
118
|
+
if (type === 'Boolean') {
|
|
119
|
+
next = val === true || val === '';
|
|
120
|
+
} else if (type === 'Number' && val !== null && val !== undefined && val !== '') {
|
|
121
|
+
next = Number(val);
|
|
122
|
+
}
|
|
123
|
+
this._props[key] = next;
|
|
124
|
+
|
|
125
|
+
// Only reflect primitives — objects/arrays stay as properties (no [object Object] attrs)
|
|
126
|
+
const isPrimitive =
|
|
127
|
+
next === null ||
|
|
128
|
+
next === undefined ||
|
|
129
|
+
typeof next === 'string' ||
|
|
130
|
+
typeof next === 'number' ||
|
|
131
|
+
typeof next === 'boolean';
|
|
132
|
+
|
|
133
|
+
this._reflecting = true;
|
|
134
|
+
try {
|
|
135
|
+
if (type === 'Boolean') {
|
|
136
|
+
if (next) this.setAttribute(key, '');
|
|
137
|
+
else this.removeAttribute(key);
|
|
138
|
+
} else if (!isPrimitive) {
|
|
139
|
+
// property-only; drop stale attribute if any
|
|
140
|
+
this.removeAttribute(key);
|
|
141
|
+
} else if (next === null || next === undefined) {
|
|
142
|
+
this.removeAttribute(key);
|
|
143
|
+
} else {
|
|
144
|
+
this.setAttribute(key, String(next));
|
|
145
|
+
}
|
|
146
|
+
} finally {
|
|
147
|
+
this._reflecting = false;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
_emitChange() {
|
|
153
|
+
// Shallow-copy both bags so listeners cannot mutate internal state/props via event.detail
|
|
154
|
+
this.dispatchEvent(new CustomEvent('kitbash-change', {
|
|
155
|
+
detail: {
|
|
156
|
+
state: { ...this._state },
|
|
157
|
+
props: { ...this._props },
|
|
158
|
+
},
|
|
159
|
+
bubbles: true,
|
|
160
|
+
composed: true
|
|
161
|
+
}));
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Batch props and/or state: one uhtml update, one kitbash-change.
|
|
166
|
+
* Prefer this in event handlers (e.g. controlled inputs).
|
|
167
|
+
*/
|
|
168
|
+
commit(patch) {
|
|
169
|
+
const p = patch || {};
|
|
170
|
+
if (p.props) {
|
|
171
|
+
const allowed = this.constructor.propTypes || {};
|
|
172
|
+
for (const key of Object.keys(p.props)) {
|
|
173
|
+
// Only declared props — ignore typos / stray keys
|
|
174
|
+
if (!Object.prototype.hasOwnProperty.call(allowed, key)) continue;
|
|
175
|
+
this._assignProp(key, p.props[key]);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
if (p.state) {
|
|
179
|
+
this._state = { ...this._state, ...p.state };
|
|
180
|
+
}
|
|
181
|
+
this.update();
|
|
182
|
+
this._emitChange();
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
setState(newState) {
|
|
186
|
+
this.commit({ state: newState });
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
setProps(nextProps) {
|
|
190
|
+
this.commit({ props: nextProps });
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
_handlerCtx() {
|
|
194
|
+
// Snapshots so handlers/render cannot mutate internal bags by accident
|
|
195
|
+
return {
|
|
196
|
+
props: { ...this._props },
|
|
197
|
+
state: { ...this._state },
|
|
198
|
+
setState: this.setState.bind(this),
|
|
199
|
+
setProps: this.setProps.bind(this),
|
|
200
|
+
commit: this.commit.bind(this),
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
cleanupEvents() {
|
|
205
|
+
this._eventCleanup.forEach(cleanup => cleanup());
|
|
206
|
+
this._eventCleanup = [];
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
update() {
|
|
210
|
+
// Run the uhtml DOM diffing render cycle
|
|
211
|
+
render(this.shadowRoot, this._renderFn({
|
|
212
|
+
...this._handlerCtx(),
|
|
213
|
+
html
|
|
214
|
+
}));
|
|
215
|
+
this.cleanupEvents();
|
|
216
|
+
this.bindEvents();
|
|
217
|
+
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
bindEvents() {
|
|
223
|
+
Object.keys(this._eventHandlers).forEach(eventSelector => {
|
|
224
|
+
const parts = eventSelector.split(' ');
|
|
225
|
+
const eventName = parts[0];
|
|
226
|
+
const selector = parts.slice(1).join(' ');
|
|
227
|
+
|
|
228
|
+
const targets = selector ? this.shadowRoot.querySelectorAll(selector) : [this];
|
|
229
|
+
targets.forEach(target => {
|
|
230
|
+
const handler = (e) => {
|
|
231
|
+
this._eventHandlers[eventSelector](e, this._handlerCtx());
|
|
232
|
+
};
|
|
233
|
+
target.addEventListener(eventName, handler);
|
|
234
|
+
this._eventCleanup.push(() => target.removeEventListener(eventName, handler));
|
|
235
|
+
});
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (!customElements.get('kitbash-theme-toggle')) {
|
|
241
|
+
customElements.define('kitbash-theme-toggle', KitbashThemeToggle);
|
|
242
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
var d=(_,U,B,Y,z)=>{let J=B.length,O=U.length,I=J,Q=0,$=0,X=null;while(Q<O||$<I)if(O===Q){let W=I<J?$?Y(B[$-1],-0).nextSibling:Y(B[I],0):z;while($<I)_.insertBefore(Y(B[$++],1),W)}else if(I===$)while(Q<O){if(!X||!X.has(U[Q]))_.removeChild(Y(U[Q],-1));Q++}else if(U[Q]===B[$])Q++,$++;else if(U[O-1]===B[I-1])O--,I--;else if(U[Q]===B[I-1]&&B[$]===U[O-1]){let W=Y(U[--O],-0).nextSibling;_.insertBefore(Y(B[$++],1),Y(U[Q++],-0).nextSibling),_.insertBefore(Y(B[--I],1),W),U[O]=B[I]}else{if(!X){X=new Map;let W=$;while(W<I)X.set(B[W],W++)}if(X.has(U[Q])){let W=X.get(U[Q]);if($<W&&W<I){let Z=Q,K=1;while(++Z<O&&Z<I&&X.get(U[Z])===W+K)K++;if(K>W-$){let M=Y(U[Q],0);while($<W)_.insertBefore(Y(B[$++],1),M)}else _.replaceChild(Y(B[$++],1),Y(U[Q++],-1))}else Q++}else _.removeChild(Y(U[Q++],-1))}return B};var{isArray:E}=Array,{getPrototypeOf:D1,getOwnPropertyDescriptor:M1}=Object;var m="http://www.w3.org/2000/svg",q=[],N=()=>document.createRange(),G=(_,U,B)=>{return _.set(U,B),B},i=(_,U)=>{let B;do B=M1(_,U);while(!B&&(_=D1(_)));return B},o=(_,U)=>U.reduceRight(G1,_),G1=(_,U)=>_.childNodes[U];var s=1;var r=8;var l=11;var{setPrototypeOf:T1}=Object,n=(_)=>{function U(B){return T1(B,new.target.prototype)}return U.prototype=_.prototype,U};var T,V=(_,U,B)=>{if(!T)T=N();if(B)T.setStartAfter(_);else T.setStartBefore(_);return T.setEndAfter(U),T.deleteContents(),_};var x=({firstChild:_,lastChild:U},B)=>V(_,U,B),t=!1,j=(_,U)=>t&&_.nodeType===l?1/U<0?U?x(_,!0):_.lastChild:U?_.valueOf():_.firstChild:_,a=(_)=>document.createComment(_);class w extends n(DocumentFragment){#U=a("<>");#B=a("</>");#_=q;constructor(_){super(_);this.replaceChildren(...[this.#U,..._.childNodes,this.#B]),t=!0}get firstChild(){return this.#U}get lastChild(){return this.#B}get parentNode(){return this.#U.parentNode}remove(){x(this,!1)}replaceWith(_){x(this,!0).replaceWith(_)}valueOf(){let{parentNode:_}=this;if(_===this){if(this.#_===q)this.#_=[...this.childNodes]}else{if(_){let{firstChild:U,lastChild:B}=this;this.#_=[U];while(U!==B)this.#_.push(U=U.nextSibling)}this.replaceChildren(...this.#_)}return this}}var e=(_,U,B)=>_.setAttribute(U,B),P=(_,U)=>_.removeAttribute(U),H1=(_,U)=>{for(let B in U){let Y=U[B],z=B==="role"?B:`aria-${B}`;if(Y==null)P(_,z);else e(_,z,Y)}return U},C,P1=(_,U,B)=>{if(B=B.slice(1),!C)C=new WeakMap;let Y=C.get(_)||G(C,_,{}),z=Y[B];if(z&&z[0])_.removeEventListener(B,...z);if(z=E(U)?U:[U,!1],Y[B]=z,z[0])_.addEventListener(B,...z);return U};var L=(_,U)=>{let{t:B,n:Y}=_,z=!1;switch(typeof U){case"object":if(U!==null){(Y||B).replaceWith(_.n=U.valueOf());break}case"undefined":z=!0;default:if(B.data=z?"":U,Y)_.n=null,Y.replaceWith(B);break}return U},F1=(_,U)=>f(_,U,U==null?"class":"className"),A1=(_,U)=>{let{dataset:B}=_;for(let Y in U)if(U[Y]==null)delete B[Y];else B[Y]=U[Y];return U},k=(_,U,B)=>_[B]=U,E1=(_,U,B)=>k(_,U,B.slice(1)),f=(_,U,B)=>U==null?(P(_,B),U):k(_,U,B),_1=(_,U)=>(typeof U==="function"?U(_):U.current=_,U),y=(_,U,B)=>(U==null?P(_,B):e(_,B,U),U),N1=(_,U)=>U==null?f(_,U,"style"):k(_.style,U,"cssText"),V1=(_,U,B)=>(_.toggleAttribute(B.slice(1),U),U),D=(_,U,B)=>{let{length:Y}=U;if(_.data=`[${Y}]`,Y)return d(_.parentNode,B,U,j,_);switch(B.length){case 1:B[0].remove();case 0:break;default:V(j(B[0],0),j(B.at(-1),-0),!1);break}return q},U1=new Map([["aria",H1],["class",F1],["data",A1],["ref",_1],["style",N1]]),B1=(_,U,B)=>{switch(U[0]){case".":return E1;case"?":return V1;case"@":return P1;default:return B||"ownerSVGElement"in _?U==="ref"?_1:y:U1.get(U)||(U in _?U.startsWith("on")?k:i(_,U)?.set?f:y:y)}},Y1=(_,U)=>(_.textContent=U==null?"":U,U);var H=(_,U,B)=>({a:_,b:U,c:B}),z1=(_,U)=>({b:_,c:U}),I1=(_,U,B,Y)=>({v:q,u:_,t:U,n:B,c:Y}),R=()=>H(null,null,q);var b=(_)=>(U,B)=>{let{a:Y,b:z,c:J}=_(U,B),O=document.importNode(Y,!0),I=q;if(z!==q){I=[];for(let Q,$,X=0;X<z.length;X++){let{a:W,b:Z,c:K}=z[X],M=W===$?Q:Q=o(O,$=W);I[X]=I1(Z,M,K,Z===D?[]:Z===L?R():null)}}return z1(J?O.firstChild:new w(O),I)};var J1=/^(?:plaintext|script|style|textarea|title|xmp)$/i,O1=/^(?:area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr)$/i;var j1=/<([a-zA-Z0-9]+[a-zA-Z0-9:._-]*)([^>]*?)(\/?)>/g,C1=/([^\s\\>"'=]+)\s*=\s*(['"]?)\x01/g,k1=/[\x01\x02]/g,Q1=(_,U,B)=>{let Y=0;return _.join("\x01").trim().replace(j1,(z,J,O,I)=>`<${J}${O.replace(C1,"\x02=$2$1").trimEnd()}${I?B||O1.test(J)?" /":`></${J}`:""}>`).replace(k1,(z)=>z==="\x01"?`<!--${U+Y++}-->`:U+Y++)};var S=document.createElement("template"),g,h,W1=(_,U)=>{if(U){if(!g)g=document.createElementNS(m,"svg"),h=N(),h.selectNodeContents(g);return h.createContextualFragment(_)}S.innerHTML=_;let{content:B}=S;return S=S.cloneNode(!1),B};var c=(_)=>{let U=[],B;while(B=_.parentNode)U.push(U.indexOf.call(B.childNodes,_)),_=B;return U},X1=()=>document.createTextNode(""),S1=(_,U,B)=>{let Y=W1(Q1(_,F,B),B),{length:z}=_,J=q;if(z>1){let Q=[],$=document.createTreeWalker(Y,129),X=0,W=`${F}${X++}`;J=[];while(X<z){let Z=$.nextNode();if(Z.nodeType===r){if(Z.data===W){let K=E(U[X-1])?D:L;if(K===L)Q.push(Z);J.push(H(c(Z),K,null)),W=`${F}${X++}`}}else{let K;while(Z.hasAttribute(W)){if(!K)K=c(Z);let M=Z.getAttribute(W);J.push(H(K,B1(Z,M,B),M)),P(Z,W),W=`${F}${X++}`}if(!B&&J1.test(Z.localName)&&Z.textContent.trim()===`<!--${W}-->`)J.push(H(K||c(Z),Y1,null)),W=`${F}${X++}`}}for(X=0;X<Q.length;X++)Q[X].replaceWith(X1())}let{childNodes:O}=Y,{length:I}=O;if(I<1)I=1,Y.appendChild(X1());else if(I===1&&z!==1&&O[0].nodeType!==s)I=0;return G($1,_,H(Y,J,I===1))},$1=new WeakMap,F="isµ",v=(_)=>(U,B)=>$1.get(U)||S1(U,B,_);var x1=b(v(!1)),w1=b(v(!0)),p=(_,{s:U,t:B,v:Y})=>{if(_.a!==B){let{b:z,c:J}=(U?w1:x1)(B,Y);_.a=B,_.b=z,_.c=J}for(let{c:z}=_,J=0;J<z.length;J++){let O=Y[J],I=z[J];switch(I.u){case D:I.v=D(I.t,y1(I.c,O),I.v);break;case L:let Q=O instanceof A?p(I.c||(I.c=R()),O):(I.c=null,O);if(Q!==I.v)I.v=L(I,Q);break;default:if(O!==I.v)I.v=I.u(I.t,O,I.n,I.v);break}}return _.b},y1=(_,U)=>{let B=0,{length:Y}=U;if(Y<_.length)_.splice(Y);for(;B<Y;B++){let z=U[B];if(z instanceof A)U[B]=p(_[B]||(_[B]=R()),z);else _[B]=null}return U};class A{constructor(_,U,B){this.s=_,this.t=U,this.v=B}toDOM(_=R()){return p(_,this)}}var Z1=new WeakMap,u=(_,U)=>{let B=Z1.get(_)||G(Z1,_,R()),{b:Y}=B;if(Y!==(typeof U==="function"?U():U).toDOM(B))_.replaceChildren(B.b.valueOf());return _};/*! (c) Andrea Giammarchi - MIT */var K1=(_)=>(U,...B)=>new A(_,U,B),q1=K1(!1),C_=K1(!0);var L1=new CSSStyleSheet;L1.replaceSync(`
|
|
2
|
+
|
|
3
|
+
:host {
|
|
4
|
+
display: inline-block;
|
|
5
|
+
font-family: var(--kb-font-family-sans);
|
|
6
|
+
}
|
|
7
|
+
[part='group-root'] {
|
|
8
|
+
display: inline-flex;
|
|
9
|
+
align-items: center;
|
|
10
|
+
flex-wrap: wrap;
|
|
11
|
+
}
|
|
12
|
+
slot {
|
|
13
|
+
display: contents;
|
|
14
|
+
}
|
|
15
|
+
`);class R1 extends HTMLElement{static get observedAttributes(){return[]}static get propTypes(){return{}}constructor(){super();this.attachShadow({mode:"open",delegatesFocus:!1}),this.shadowRoot.adoptedStyleSheets=[L1],this._defaults={},this._state={},this._props={...this._defaults},this._eventHandlers={"slotchange slot":function(_){let U=_.target.assignedElements({flatten:!0}).filter((Y)=>Y instanceof HTMLElement),B=U.length;U.forEach((Y,z)=>{if(B===1){Y.removeAttribute("data-group-pos");return}let J="mid";if(z===0)J="start";else if(z===B-1)J="end";Y.setAttribute("data-group-pos",J)})}},this._eventCleanup=[],this._renderFn=function({html:_}){return _`
|
|
16
|
+
<div part="group-root" role="group" aria-label="Display options">
|
|
17
|
+
<slot></slot>
|
|
18
|
+
</div>
|
|
19
|
+
`},this._reflecting=!1}connectedCallback(){this.update()}disconnectedCallback(){this.cleanupEvents()}attributeChangedCallback(_,U,B){if(this._reflecting)return;if(U===B)return;let Y=this.constructor.propTypes[_],z=B;if(B===null||B===void 0)z=this._defaults[_];else if(Y==="Number"&&B!=="")z=Number(B);else if(Y==="Boolean")z=B!==null&&B!=="false";this._props[_]=z,this.update()}_assignProp(_,U){let B=this.constructor.propTypes[_],Y=U;if(B==="Boolean")Y=U===!0||U==="";else if(B==="Number"&&U!==null&&U!==void 0&&U!=="")Y=Number(U);this._props[_]=Y;let z=Y===null||Y===void 0||typeof Y==="string"||typeof Y==="number"||typeof Y==="boolean";this._reflecting=!0;try{if(B==="Boolean")if(Y)this.setAttribute(_,"");else this.removeAttribute(_);else if(!z)this.removeAttribute(_);else if(Y===null||Y===void 0)this.removeAttribute(_);else this.setAttribute(_,String(Y))}finally{this._reflecting=!1}}_emitChange(){this.dispatchEvent(new CustomEvent("kitbash-change",{detail:{state:{...this._state},props:{...this._props}},bubbles:!0,composed:!0}))}commit(_){let U=_||{};if(U.props){let B=this.constructor.propTypes||{};for(let Y of Object.keys(U.props)){if(!Object.prototype.hasOwnProperty.call(B,Y))continue;this._assignProp(Y,U.props[Y])}}if(U.state)this._state={...this._state,...U.state};this.update(),this._emitChange()}setState(_){this.commit({state:_})}setProps(_){this.commit({props:_})}_handlerCtx(){return{props:{...this._props},state:{...this._state},setState:this.setState.bind(this),setProps:this.setProps.bind(this),commit:this.commit.bind(this)}}cleanupEvents(){this._eventCleanup.forEach((_)=>_()),this._eventCleanup=[]}update(){u(this.shadowRoot,this._renderFn({...this._handlerCtx(),html:q1})),this.cleanupEvents(),this.bindEvents()}bindEvents(){Object.keys(this._eventHandlers).forEach((_)=>{let U=_.split(" "),B=U[0],Y=U.slice(1).join(" ");(Y?this.shadowRoot.querySelectorAll(Y):[this]).forEach((J)=>{let O=(I)=>{this._eventHandlers[_](I,this._handlerCtx())};J.addEventListener(B,O),this._eventCleanup.push(()=>J.removeEventListener(B,O))})})}}if(!customElements.get("kitbash-toggle-group"))customElements.define("kitbash-toggle-group",R1);export{R1 as KitbashToggleGroup};
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
|
|
2
|
+
import { render, html } from 'uhtml';
|
|
3
|
+
|
|
4
|
+
const sheet = new CSSStyleSheet();
|
|
5
|
+
sheet.replaceSync("\n\n :host {\n display: inline-block;\n font-family: var(--kb-font-family-sans);\n }\n [part='group-root'] {\n display: inline-flex;\n align-items: center;\n flex-wrap: wrap;\n }\n slot {\n display: contents;\n }\n ");
|
|
6
|
+
|
|
7
|
+
export class KitbashToggleGroup extends HTMLElement {
|
|
8
|
+
|
|
9
|
+
static get observedAttributes() {
|
|
10
|
+
return [];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
static get propTypes() {
|
|
14
|
+
return {};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
constructor() {
|
|
20
|
+
super();
|
|
21
|
+
this.attachShadow({ mode: 'open', delegatesFocus: false });
|
|
22
|
+
|
|
23
|
+
this.shadowRoot.adoptedStyleSheets = [sheet];
|
|
24
|
+
|
|
25
|
+
this._defaults = {};
|
|
26
|
+
|
|
27
|
+
this._state = {};
|
|
28
|
+
this._props = { ...this._defaults };
|
|
29
|
+
this._eventHandlers = { 'slotchange slot': function(e) {
|
|
30
|
+
const els = e.target.assignedElements({ flatten: !0 }).filter((n) => n instanceof HTMLElement), n = els.length;
|
|
31
|
+
els.forEach((el, i) => {
|
|
32
|
+
if (n === 1) {
|
|
33
|
+
el.removeAttribute("data-group-pos");
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
let pos = "mid";
|
|
37
|
+
if (i === 0)
|
|
38
|
+
pos = "start";
|
|
39
|
+
else if (i === n - 1)
|
|
40
|
+
pos = "end";
|
|
41
|
+
el.setAttribute("data-group-pos", pos);
|
|
42
|
+
});
|
|
43
|
+
} };
|
|
44
|
+
this._eventCleanup = [];
|
|
45
|
+
this._renderFn = function({ html }) {
|
|
46
|
+
return html`
|
|
47
|
+
<div part="group-root" role="group" aria-label="Display options">
|
|
48
|
+
<slot></slot>
|
|
49
|
+
</div>
|
|
50
|
+
`;
|
|
51
|
+
};
|
|
52
|
+
this._reflecting = false;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
connectedCallback() {
|
|
56
|
+
// Initial attribute values are parsed by attributeChangedCallback before connectedCallback fires
|
|
57
|
+
this.update();
|
|
58
|
+
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
disconnectedCallback() {
|
|
62
|
+
this.cleanupEvents();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
attributeChangedCallback(name, oldValue, newValue) {
|
|
66
|
+
// Skip when we caused the attribute write ourselves (avoids double update)
|
|
67
|
+
if (this._reflecting) return;
|
|
68
|
+
if (oldValue === newValue) return;
|
|
69
|
+
|
|
70
|
+
const type = this.constructor.propTypes[name];
|
|
71
|
+
|
|
72
|
+
let parsedValue = newValue;
|
|
73
|
+
if (newValue === null || newValue === undefined) {
|
|
74
|
+
parsedValue = this._defaults[name];
|
|
75
|
+
} else if (type === 'Number' && newValue !== '') {
|
|
76
|
+
// Match property coercion: empty string is not Number('') → 0
|
|
77
|
+
parsedValue = Number(newValue);
|
|
78
|
+
} else if (type === 'Boolean') {
|
|
79
|
+
parsedValue = newValue !== null && newValue !== 'false';
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
this._props[name] = parsedValue;
|
|
83
|
+
|
|
84
|
+
this.update();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Coerce + reflect a single prop; no re-render (caller decides). */
|
|
88
|
+
_assignProp(key, val) {
|
|
89
|
+
const type = this.constructor.propTypes[key];
|
|
90
|
+
let next = val;
|
|
91
|
+
if (type === 'Boolean') {
|
|
92
|
+
next = val === true || val === '';
|
|
93
|
+
} else if (type === 'Number' && val !== null && val !== undefined && val !== '') {
|
|
94
|
+
next = Number(val);
|
|
95
|
+
}
|
|
96
|
+
this._props[key] = next;
|
|
97
|
+
|
|
98
|
+
// Only reflect primitives — objects/arrays stay as properties (no [object Object] attrs)
|
|
99
|
+
const isPrimitive =
|
|
100
|
+
next === null ||
|
|
101
|
+
next === undefined ||
|
|
102
|
+
typeof next === 'string' ||
|
|
103
|
+
typeof next === 'number' ||
|
|
104
|
+
typeof next === 'boolean';
|
|
105
|
+
|
|
106
|
+
this._reflecting = true;
|
|
107
|
+
try {
|
|
108
|
+
if (type === 'Boolean') {
|
|
109
|
+
if (next) this.setAttribute(key, '');
|
|
110
|
+
else this.removeAttribute(key);
|
|
111
|
+
} else if (!isPrimitive) {
|
|
112
|
+
// property-only; drop stale attribute if any
|
|
113
|
+
this.removeAttribute(key);
|
|
114
|
+
} else if (next === null || next === undefined) {
|
|
115
|
+
this.removeAttribute(key);
|
|
116
|
+
} else {
|
|
117
|
+
this.setAttribute(key, String(next));
|
|
118
|
+
}
|
|
119
|
+
} finally {
|
|
120
|
+
this._reflecting = false;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
_emitChange() {
|
|
126
|
+
// Shallow-copy both bags so listeners cannot mutate internal state/props via event.detail
|
|
127
|
+
this.dispatchEvent(new CustomEvent('kitbash-change', {
|
|
128
|
+
detail: {
|
|
129
|
+
state: { ...this._state },
|
|
130
|
+
props: { ...this._props },
|
|
131
|
+
},
|
|
132
|
+
bubbles: true,
|
|
133
|
+
composed: true
|
|
134
|
+
}));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Batch props and/or state: one uhtml update, one kitbash-change.
|
|
139
|
+
* Prefer this in event handlers (e.g. controlled inputs).
|
|
140
|
+
*/
|
|
141
|
+
commit(patch) {
|
|
142
|
+
const p = patch || {};
|
|
143
|
+
if (p.props) {
|
|
144
|
+
const allowed = this.constructor.propTypes || {};
|
|
145
|
+
for (const key of Object.keys(p.props)) {
|
|
146
|
+
// Only declared props — ignore typos / stray keys
|
|
147
|
+
if (!Object.prototype.hasOwnProperty.call(allowed, key)) continue;
|
|
148
|
+
this._assignProp(key, p.props[key]);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (p.state) {
|
|
152
|
+
this._state = { ...this._state, ...p.state };
|
|
153
|
+
}
|
|
154
|
+
this.update();
|
|
155
|
+
this._emitChange();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
setState(newState) {
|
|
159
|
+
this.commit({ state: newState });
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
setProps(nextProps) {
|
|
163
|
+
this.commit({ props: nextProps });
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
_handlerCtx() {
|
|
167
|
+
// Snapshots so handlers/render cannot mutate internal bags by accident
|
|
168
|
+
return {
|
|
169
|
+
props: { ...this._props },
|
|
170
|
+
state: { ...this._state },
|
|
171
|
+
setState: this.setState.bind(this),
|
|
172
|
+
setProps: this.setProps.bind(this),
|
|
173
|
+
commit: this.commit.bind(this),
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
cleanupEvents() {
|
|
178
|
+
this._eventCleanup.forEach(cleanup => cleanup());
|
|
179
|
+
this._eventCleanup = [];
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
update() {
|
|
183
|
+
// Run the uhtml DOM diffing render cycle
|
|
184
|
+
render(this.shadowRoot, this._renderFn({
|
|
185
|
+
...this._handlerCtx(),
|
|
186
|
+
html
|
|
187
|
+
}));
|
|
188
|
+
this.cleanupEvents();
|
|
189
|
+
this.bindEvents();
|
|
190
|
+
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
bindEvents() {
|
|
196
|
+
Object.keys(this._eventHandlers).forEach(eventSelector => {
|
|
197
|
+
const parts = eventSelector.split(' ');
|
|
198
|
+
const eventName = parts[0];
|
|
199
|
+
const selector = parts.slice(1).join(' ');
|
|
200
|
+
|
|
201
|
+
const targets = selector ? this.shadowRoot.querySelectorAll(selector) : [this];
|
|
202
|
+
targets.forEach(target => {
|
|
203
|
+
const handler = (e) => {
|
|
204
|
+
this._eventHandlers[eventSelector](e, this._handlerCtx());
|
|
205
|
+
};
|
|
206
|
+
target.addEventListener(eventName, handler);
|
|
207
|
+
this._eventCleanup.push(() => target.removeEventListener(eventName, handler));
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (!customElements.get('kitbash-toggle-group')) {
|
|
214
|
+
customElements.define('kitbash-toggle-group', KitbashToggleGroup);
|
|
215
|
+
}
|