@openelement/ui 1.0.0-alpha.15 → 1.0.0-alpha.16

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.
@@ -1,255 +1,118 @@
1
- import { CODE_BLOCK_CONSTANTS, log } from "./component-recipes.js";
2
- import { OpenElement } from "@openelement/element";
3
- import openCodeBlockStyles from "./open-code-block.css";
1
+ import { defineComponentConfig } from "./component-config.js";
2
+ import { log } from "./component-recipes.js";
3
+ import { cancelCopyReset, scheduleCopyReset } from "./copy-feedback.js";
4
+ import { jsx, jsxs } from "@openelement/element/jsx-runtime";
5
+ import { onConnect, onDispose, onPropertyChange } from "@openelement/element/lifecycle";
6
+ import { computed, signal } from "@openelement/element";
4
7
  //#region src/open-code-block.tsx
5
- const __partProgram = {
6
- "version": 2,
7
- "tag": "open-code-block",
8
- "root": {
9
- "id": "root",
10
- "kind": "shadow-open",
11
- "nodes": ["e0"]
12
- },
13
- "template": [{
14
- "k": "el",
15
- "id": "e0",
16
- "tag": "div",
17
- "attrs": [["style", "display:contents"]],
18
- "children": [{
19
- "k": "el",
20
- "id": "e1",
21
- "tag": "slot",
22
- "attrs": [],
23
- "children": []
24
- }, {
25
- "k": "el",
26
- "id": "e2",
27
- "tag": "button",
28
- "attrs": [
29
- ["type", "button"],
30
- ["class", "copy-btn absolute top-2 right-2 z-10 cursor-pointer rounded-md border-[0.5px] border-transparent bg-primary/14 px-3 py-1 font-sans text-xs font-semibold tracking-wider text-foreground transition-all duration-150 ease-in-out hover:border-primary hover:bg-primary/23"],
31
- ["part", "copy"]
32
- ],
33
- "children": [{
34
- "k": "part",
35
- "id": "p1",
36
- "index": 1
37
- }]
38
- }]
39
- }],
40
- "parts": [{
41
- "k": "event",
42
- "event": "click",
43
- "handler": "copy",
44
- "action": {
45
- "kind": "method",
46
- "name": "copy"
47
- },
48
- "path": [0, 1],
49
- "index": 0,
50
- "location": {
51
- "id": "p0",
52
- "kind": "sink",
53
- "path": [0, 1],
54
- "node": "e2"
8
+ /**
9
+ * @openelement/ui - open-code-block
10
+ *
11
+ * Code block with a copy button.
12
+ *
13
+ * Contract (shrunk in #1552): the component owns the copy interaction only —
14
+ * it does NOT bundle, load, or invoke a tokenizer. Syntax highlighting is the
15
+ * host page's build-time concern: fences are compiled to token-span HTML by
16
+ * the site's markdown pipeline (e.g., Shiki) and project through the slot
17
+ * untouched. The projected content stays light DOM, so the host's stylesheet
18
+ * owns the code surface and token inks; this component only paints its chip.
19
+ *
20
+ * Generated authoring: the shell (slot + copy button) is the compiled
21
+ * template; the copy label is a compiled text sink driven by the `copyLabel`
22
+ * property, and the copy feedback's custom state (`:state(copied)` /
23
+ * `:state(failed)`) is driven from the property-change channel.
24
+ *
25
+ * @csspart copy - The copy button
26
+ *
27
+ * Usage:
28
+ * ```html
29
+ * <open-code-block>
30
+ * <pre><code>const x = 1;</code></pre>
31
+ * </open-code-block>
32
+ * ```
33
+ */
34
+ /**
35
+ * Platform facts for the generated element: shadow root, the authored
36
+ * component stylesheet, and the `copyLabel` property (property-only, no
37
+ * attribute form).
38
+ */
39
+ const openElement = /* @__PURE__ */ defineComponentConfig({
40
+ root: "shadow",
41
+ styles: ["./open-code-block.css"],
42
+ properties: { copyLabel: {
43
+ default: "Copy",
44
+ attribute: false
45
+ } }
46
+ });
47
+ /** Code block with a copy button. */
48
+ function OpenCodeBlock(props) {
49
+ const chinese = signal(false);
50
+ const translatedLabel = computed(() => !chinese.value ? props.copyLabel : props.copyLabel === "Copy" ? "复制" : props.copyLabel === "Copied!" ? "已复制" : props.copyLabel === "Failed" ? "复制失败" : props.copyLabel);
51
+ onConnect((env) => {
52
+ globalThis.queueMicrotask(() => {
53
+ if (env.signal.aborted) return;
54
+ const language = env.host.closest("[lang]")?.getAttribute("lang") ?? env.host.ownerDocument.documentElement.lang;
55
+ chinese.value = /^zh(?:-|$)/i.test(language);
56
+ });
57
+ });
58
+ /**
59
+ * Copy the slotted light DOM: token spans (if the host highlighted at build
60
+ * time) concatenate to the exact source text. The clipboard promise chain
61
+ * writes the declared `copyLabel`; the change channel below owns state and
62
+ * timer bookkeeping, so the click path never touches internals.
63
+ */
64
+ const copy = (host) => {
65
+ const text = host.textContent || "";
66
+ try {
67
+ globalThis.navigator.clipboard.writeText(text).then(() => {
68
+ host.copyLabel = "Copied!";
69
+ }, (error) => {
70
+ log.warn("Clipboard write failed:", error);
71
+ host.copyLabel = "Failed";
72
+ });
73
+ } catch (error) {
74
+ log.warn("Clipboard write failed:", error);
75
+ host.copyLabel = "Failed";
55
76
  }
56
- }, {
57
- "k": "text",
58
- "signal": "copyLabel",
59
- "index": 1,
60
- "location": {
61
- "id": "p1",
62
- "kind": "anchor",
63
- "path": [
64
- 0,
65
- 1,
66
- 0
67
- ]
77
+ };
78
+ onPropertyChange((env) => {
79
+ if (env.name !== "copyLabel") return;
80
+ if (env.value === "Copied!") {
81
+ env.states.add("copied");
82
+ env.states.delete("failed");
83
+ scheduleCopyReset(env.host);
84
+ return;
68
85
  }
69
- }],
70
- "blocks": [],
71
- "dependencies": [{
72
- "signal": "copyLabel",
73
- "owner": {
74
- "kind": "part",
75
- "index": 1
76
- },
77
- "location": "p1"
78
- }],
79
- "locations": [
80
- {
81
- "id": "e0",
82
- "kind": "element",
83
- "tag": "div",
84
- "path": [0]
85
- },
86
- {
87
- "id": "e1",
88
- "kind": "element",
89
- "tag": "slot",
90
- "path": [0, 0]
91
- },
92
- {
93
- "id": "e2",
94
- "kind": "element",
95
- "tag": "button",
96
- "path": [0, 1]
97
- },
98
- {
99
- "id": "p0",
100
- "kind": "sink",
101
- "part": 0,
102
- "node": "e2",
103
- "path": [0, 1]
104
- },
105
- {
106
- "id": "p1",
107
- "kind": "anchor",
108
- "part": 1,
109
- "path": [
110
- 0,
111
- 1,
112
- 0
113
- ]
86
+ if (env.value === "Failed") {
87
+ env.states.add("failed");
88
+ env.states.delete("copied");
89
+ scheduleCopyReset(env.host);
90
+ return;
114
91
  }
115
- ],
116
- "metadata": {
117
- "tag": "open-code-block",
118
- "className": "OpenCodeBlock",
119
- "sourceFile": "open-code-block.tsx",
120
- "properties": [{
121
- "name": "copyLabel",
122
- "attribute": null,
123
- "type": "string",
124
- "converter": "string",
125
- "reflect": false,
126
- "default": "Copy"
127
- }],
128
- "observedAttributes": [],
129
- "cem": {
130
- "tagName": "open-code-block",
131
- "className": "OpenCodeBlock",
132
- "declaration": {
133
- "name": "OpenCodeBlock",
134
- "module": "open-code-block.tsx"
92
+ env.states.delete("copied");
93
+ env.states.delete("failed");
94
+ cancelCopyReset(env.host);
95
+ });
96
+ onDispose((env) => {
97
+ cancelCopyReset(env.host);
98
+ });
99
+ return /* @__PURE__ */ jsxs("div", {
100
+ style: "display:contents",
101
+ children: [/* @__PURE__ */ jsx("slot", {}), /* @__PURE__ */ jsx("button", {
102
+ type: "button",
103
+ class: "copy-btn absolute top-2 right-2 z-10 cursor-pointer rounded-md border-[0.5px] border-transparent bg-primary/14 px-3 py-1 font-sans text-xs font-semibold tracking-wider text-foreground transition-all duration-150 ease-in-out hover:border-primary hover:bg-primary/23",
104
+ part: "copy",
105
+ onClick: (event) => {
106
+ const target = event.currentTarget;
107
+ copy(target.getRootNode().host);
135
108
  },
136
- "attributes": [],
137
- "members": [{
138
- "name": "copyLabel",
139
- "fieldName": "copyLabel",
140
- "type": "string",
141
- "attribute": null,
142
- "reflect": false
143
- }]
144
- }
145
- }
146
- };
147
- const __compiledProperties = [{
148
- "name": "copyLabel",
149
- "attribute": null,
150
- "type": "string",
151
- "converter": "string",
152
- "reflect": false,
153
- "default": "Copy"
154
- }];
155
- const __elementMetadata = {
156
- "tag": "open-code-block",
157
- "className": "OpenCodeBlock",
158
- "sourceFile": "open-code-block.tsx",
159
- "properties": [{
160
- "name": "copyLabel",
161
- "attribute": null,
162
- "type": "string",
163
- "converter": "string",
164
- "reflect": false,
165
- "default": "Copy"
166
- }],
167
- "observedAttributes": [],
168
- "cem": {
169
- "tagName": "open-code-block",
170
- "className": "OpenCodeBlock",
171
- "declaration": {
172
- "name": "OpenCodeBlock",
173
- "module": "open-code-block.tsx"
174
- },
175
- "attributes": [],
176
- "members": [{
177
- "name": "copyLabel",
178
- "fieldName": "copyLabel",
179
- "type": "string",
180
- "attribute": null,
181
- "reflect": false
182
- }]
183
- }
184
- };
185
- const __observedAttributes = [];
186
- const __compiledProps = { copyLabel: {
187
- type: String,
188
- default: "Copy",
189
- reflect: false,
190
- attribute: false
191
- } };
192
- var OpenCodeBlock = class extends OpenElement {
193
- static __partProgram = __partProgram;
194
- static __compiledProperties = __compiledProperties;
195
- static __elementMetadata = __elementMetadata;
196
- static props = __compiledProps;
197
- static observedAttributes = __observedAttributes;
198
- static __oeUtilityClasses = true;
199
- static __oeClassCandidates = [
200
- "copy-btn",
201
- "absolute",
202
- "top-2",
203
- "right-2",
204
- "z-10",
205
- "cursor-pointer",
206
- "rounded-md",
207
- "border-[0.5px]",
208
- "border-transparent",
209
- "bg-primary/14",
210
- "px-3",
211
- "py-1",
212
- "font-sans",
213
- "text-xs",
214
- "font-semibold",
215
- "tracking-wider",
216
- "text-foreground",
217
- "transition-all",
218
- "duration-150",
219
- "ease-in-out",
220
- "hover:border-primary",
221
- "hover:bg-primary/23"
222
- ];
223
- static styles = [openCodeBlockStyles];
224
- copyLabel = "Copy";
225
- getCodeText() {
226
- return this.textContent || "";
227
- }
228
- async copy() {
229
- try {
230
- const text = this.getCodeText();
231
- await navigator.clipboard.writeText(text);
232
- this.copyLabel = "Copied!";
233
- this._internals?.states.add("copied");
234
- this._internals?.states.delete("failed");
235
- this._setTimeout(() => {
236
- this.copyLabel = "Copy";
237
- this._internals?.states.delete("copied");
238
- }, CODE_BLOCK_CONSTANTS.copyFeedbackMs);
239
- } catch (e) {
240
- log.warn("Clipboard write failed:", e);
241
- this.copyLabel = "Failed";
242
- this._internals?.states.add("failed");
243
- this._internals?.states.delete("copied");
244
- this._setTimeout(() => {
245
- this.copyLabel = "Copy";
246
- this._internals?.states.delete("failed");
247
- }, CODE_BLOCK_CONSTANTS.copyFeedbackMs);
248
- }
249
- }
250
- render() {
251
- throw new Error("[open:compiled-element] open-code-block is compiled to a Part Program; the runtime JSX render path is not available.");
252
- }
253
- };
109
+ children: translatedLabel.value
110
+ })]
111
+ });
112
+ }
254
113
  //#endregion
255
- export { OpenCodeBlock, __elementMetadata, __partProgram };
114
+ export { OpenCodeBlock as default, openElement };
115
+
116
+ export { OpenCodeBlockElement, OpenCodeBlock__ssrHost } from './open-code-block.generated.js';
117
+
118
+ // openElement:class-candidates ["absolute","bg-primary/14","border-[0.5px]","border-transparent","copy-btn","cursor-pointer","duration-150","ease-in-out","font-sans","font-semibold","hover:bg-primary/23","hover:border-primary","px-3","py-1","right-2","rounded-md","text-foreground","text-xs","top-2","tracking-wider","transition-all","z-10"]
@@ -1,273 +1,32 @@
1
- import { OpenElement, StyleSheetLike } from "@openelement/element";
2
1
  //#region src/open-dialog.d.ts
3
- declare const __partProgram: {
4
- version: number;
5
- tag: string;
6
- root: {
7
- id: string;
8
- kind: string;
9
- nodes: string[];
10
- };
11
- template: {
12
- k: string;
13
- id: string;
14
- tag: string;
15
- attrs: string[][];
16
- children: {
17
- k: string;
18
- id: string;
19
- tag: string;
20
- attrs: string[][];
21
- children: {
22
- k: string;
23
- id: string;
24
- tag: string;
25
- attrs: string[][];
26
- children: ({
27
- k: string;
28
- id: string;
29
- tag: string;
30
- attrs: string[][];
31
- children: {
32
- k: string;
33
- id: string;
34
- index: number;
35
- }[];
36
- } | {
37
- k: string;
38
- id: string;
39
- tag: string;
40
- attrs: string[][];
41
- children: {
42
- k: string;
43
- value: string;
44
- }[];
45
- })[];
46
- }[];
47
- }[];
48
- }[];
49
- parts: ({
50
- k: string;
51
- event: string;
52
- handler: string;
53
- action: {
54
- kind: string;
55
- name: string;
56
- };
57
- path: number[];
58
- index: number;
59
- location: {
60
- id: string;
61
- kind: string;
62
- path: number[];
63
- node: string;
64
- };
65
- signal?: undefined;
66
- name?: undefined;
67
- } | {
68
- k: string;
69
- signal: string;
70
- name: string;
71
- path: number[];
72
- index: number;
73
- location: {
74
- id: string;
75
- kind: string;
76
- path: number[];
77
- node: string;
78
- };
79
- event?: undefined;
80
- handler?: undefined;
81
- action?: undefined;
82
- } | {
83
- k: string;
84
- signal: string;
85
- index: number;
86
- location: {
87
- id: string;
88
- kind: string;
89
- path: number[];
90
- node?: undefined;
2
+ /**
3
+ * Platform facts for the generated element: shadow root with delegated focus,
4
+ * the authored package stylesheets, and the declared property rows. Only
5
+ * `open` reflects property updates back to the attribute.
6
+ */
7
+ export declare const openElement: {
8
+ root: "shadow";
9
+ delegatesFocus: true;
10
+ styles: string[];
11
+ properties: {
12
+ open: {
13
+ default: false;
14
+ reflect: true;
91
15
  };
92
- event?: undefined;
93
- handler?: undefined;
94
- action?: undefined;
95
- path?: undefined;
96
- name?: undefined;
97
- })[];
98
- blocks: never[];
99
- dependencies: {
100
- signal: string;
101
- owner: {
102
- kind: string;
103
- index: number;
104
- };
105
- location: string;
106
- }[];
107
- locations: ({
108
- id: string;
109
- kind: string;
110
- tag: string;
111
- path: number[];
112
- part?: undefined;
113
- node?: undefined;
114
- } | {
115
- id: string;
116
- kind: string;
117
- part: number;
118
- node: string;
119
- path: number[];
120
- tag?: undefined;
121
- } | {
122
- id: string;
123
- kind: string;
124
- part: number;
125
- path: number[];
126
- tag?: undefined;
127
- node?: undefined;
128
- })[];
129
- metadata: {
130
- tag: string;
131
- className: string;
132
- sourceFile: string;
133
- properties: ({
134
- name: string;
135
- attribute: string;
136
- type: string;
137
- converter: string;
138
- reflect: boolean;
139
- default: boolean;
140
- } | {
141
- name: string;
142
- attribute: string;
143
- type: string;
144
- converter: string;
145
- reflect: boolean;
16
+ label: {
146
17
  default: string;
147
- })[];
148
- observedAttributes: string[];
149
- cem: {
150
- tagName: string;
151
- className: string;
152
- declaration: {
153
- name: string;
154
- module: string;
155
- };
156
- attributes: {
157
- name: string;
158
- fieldName: string;
159
- type: string;
160
- reflect: boolean;
161
- }[];
162
- members: {
163
- name: string;
164
- fieldName: string;
165
- type: string;
166
- attribute: string;
167
- reflect: boolean;
168
- }[];
169
18
  };
170
- };
171
- };
172
- declare const __compiledProperties: ({
173
- name: string;
174
- attribute: string;
175
- type: string;
176
- converter: string;
177
- reflect: boolean;
178
- default: boolean;
179
- } | {
180
- name: string;
181
- attribute: string;
182
- type: string;
183
- converter: string;
184
- reflect: boolean;
185
- default: string;
186
- })[];
187
- declare const __elementMetadata: {
188
- tag: string;
189
- className: string;
190
- sourceFile: string;
191
- properties: ({
192
- name: string;
193
- attribute: string;
194
- type: string;
195
- converter: string;
196
- reflect: boolean;
197
- default: boolean;
198
- } | {
199
- name: string;
200
- attribute: string;
201
- type: string;
202
- converter: string;
203
- reflect: boolean;
204
- default: string;
205
- })[];
206
- observedAttributes: string[];
207
- cem: {
208
- tagName: string;
209
- className: string;
210
- declaration: {
211
- name: string;
212
- module: string;
213
- };
214
- attributes: {
215
- name: string;
216
- fieldName: string;
217
- type: string;
218
- reflect: boolean;
219
- }[];
220
- members: {
221
- name: string;
222
- fieldName: string;
223
- type: string;
19
+ closeLabel: {
20
+ default: string;
224
21
  attribute: string;
225
- reflect: boolean;
226
- }[];
227
- };
228
- };
229
- declare const __observedAttributes: string[];
230
- declare const __compiledProps: {
231
- open: {
232
- type: BooleanConstructor;
233
- default: boolean;
234
- reflect: boolean;
235
- attribute: string;
236
- };
237
- label: {
238
- type: StringConstructor;
239
- default: string;
240
- reflect: boolean;
241
- attribute: string;
22
+ };
242
23
  };
243
24
  };
244
- export declare class OpenDialog extends OpenElement {
245
- static __partProgram: typeof __partProgram;
246
- static __compiledProperties: typeof __compiledProperties;
247
- static __elementMetadata: typeof __elementMetadata;
248
- static props: typeof __compiledProps;
249
- static observedAttributes: typeof __observedAttributes;
250
- static delegatesFocus: boolean;
251
- static __oeUtilityClasses: boolean;
252
- static __oeClassCandidates: readonly string[];
253
- static styles: StyleSheetLike[];
25
+ /** Dialog component using native <dialog> element + popover API. */
26
+ declare function OpenDialog(props: {
254
27
  open: boolean;
255
28
  label: string;
256
- onCsrRendered(): void;
257
- onDsdHydrated(): void;
258
- disconnectedCallback(): void;
259
- private syncOpenState;
260
- private updateStates;
261
- private stateSet;
262
- show(): void;
263
- close(): void;
264
- toggle(): void;
265
- private syncDialogElement;
266
- private handleNativeClose;
267
- private handleClose;
268
- private handleCancel;
269
- private handleTrigger;
270
- render(): never;
271
- }
29
+ closeLabel: string;
30
+ }): any;
272
31
  //#endregion
273
- export { __elementMetadata, __partProgram };
32
+ export { OpenDialog as default };