@antadesign/anta 0.3.8 → 0.3.10

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.
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Shared copy-to-clipboard behavior for `<a-button>` and `<a-menu-item>` — the
3
+ * write half of the productized "copy button" / "copying menu item". The element
4
+ * performs the clipboard write itself (rather than a UI-thread callback) and
5
+ * reports the outcome with a `copydone` event; the JSX wrapper listens and
6
+ * orchestrates the visual feedback (icon + tone swap). Nothing here touches the
7
+ * DOM — no light-DOM mutation, no host attributes, no ElementInternals.
8
+ *
9
+ * Events are dispatched non-bubbling (`bubbles: false`): the wrapper binds on the
10
+ * host itself, so it catches them in the target phase, and not bubbling keeps a
11
+ * nested copy row's `copydone` from flipping an ancestor copy row's feedback
12
+ * (the same point-to-point rule Anta's `statechange` follows).
13
+ *
14
+ * Three modes, chosen by attribute:
15
+ * - `copy="<text>"` — copy the literal string on activation.
16
+ * - `copy-node` / `copy-node="<selector>"` — copy a DOM node as rich text
17
+ * (`text/html`) + plain text. Bare `copy-node` copies the nearest ancestor
18
+ * marked `[data-copy-source]`; a value is a selector resolved with `closest()`
19
+ * (an ancestor region). The copy control is stripped from the serialized
20
+ * output (`[data-copy-node-button]`).
21
+ * - `copy-lazy` — the content isn't known until the click. Activation dispatches
22
+ * `copyrequest` whose `detail.provide(text)` the consumer calls with the value
23
+ * (synchronously, or after an `await` — the browser's transient-activation
24
+ * window still covers the write). Nothing lands in the DOM, and there's no
25
+ * attribute round-trip to reset. Takes precedence over any `copy` value.
26
+ *
27
+ * Precedence when several are set (the discriminated-union prop types make this
28
+ * unreachable from the wrappers, but hand-authored markup can): node → lazy →
29
+ * literal `copy`.
30
+ */
31
+ /**
32
+ * Perform a copy on activation (click / Enter / Space / menu select). Returns
33
+ * `true` if the element is a copy control (so the caller knows the copy path
34
+ * ran); `false` for a plain, non-copy element.
35
+ */
36
+ export declare function runCopy(el: HTMLElement): boolean;
@@ -0,0 +1,68 @@
1
+ async function writeText(text) {
2
+ try {
3
+ await navigator.clipboard.writeText(text);
4
+ return true;
5
+ } catch {
6
+ return false;
7
+ }
8
+ }
9
+ async function writeNode(node) {
10
+ const clone = node.cloneNode(true);
11
+ clone.querySelectorAll("[data-copy-node-button]").forEach((n) => n.remove());
12
+ const html = clone.outerHTML;
13
+ const text = clone.textContent ?? "";
14
+ try {
15
+ if (typeof ClipboardItem !== "undefined" && navigator.clipboard?.write) {
16
+ await navigator.clipboard.write([
17
+ new ClipboardItem({
18
+ "text/html": new Blob([html], { type: "text/html" }),
19
+ "text/plain": new Blob([text], { type: "text/plain" })
20
+ })
21
+ ]);
22
+ return true;
23
+ }
24
+ await navigator.clipboard.writeText(text);
25
+ return true;
26
+ } catch {
27
+ return false;
28
+ }
29
+ }
30
+ function resolveNodeTarget(el) {
31
+ const sel = el.getAttribute("copy-node");
32
+ if (sel == null) return null;
33
+ return el.closest(sel === "" ? "[data-copy-source]" : sel);
34
+ }
35
+ function run(el, work) {
36
+ void work.then((ok) => {
37
+ el.dispatchEvent(new CustomEvent("copydone", { bubbles: false, detail: { ok } }));
38
+ });
39
+ }
40
+ function runCopy(el) {
41
+ if (el.hasAttribute("copy-node")) {
42
+ const target = resolveNodeTarget(el);
43
+ run(el, target ? writeNode(target) : Promise.resolve(false));
44
+ return true;
45
+ }
46
+ if (el.hasAttribute("copy-lazy")) {
47
+ let done = false;
48
+ el.dispatchEvent(
49
+ new CustomEvent("copyrequest", {
50
+ bubbles: false,
51
+ detail: {
52
+ provide(text) {
53
+ if (done) return;
54
+ done = true;
55
+ run(el, writeText(String(text ?? "")));
56
+ }
57
+ }
58
+ })
59
+ );
60
+ return true;
61
+ }
62
+ if (!el.hasAttribute("copy")) return false;
63
+ run(el, writeText(el.getAttribute("copy") ?? ""));
64
+ return true;
65
+ }
66
+ export {
67
+ runCopy
68
+ };
@@ -727,6 +727,26 @@ export interface AMenuItemAttributes extends BaseAttributes {
727
727
  * a `MouseEvent`, carrying the modifier keys (e.g. `altKey`). All-lowercase so
728
728
  * React/Preact bind it to the CustomEvent. */
729
729
  onmenuselect?: (e: MouseEvent) => void;
730
+ /** Text copied to the clipboard when the row is chosen (a "copying menu item").
731
+ * The element performs the write itself on `menuselect`. */
732
+ copy?: string;
733
+ /** Copy a DOM node instead of a string (rich `text/html` + plain text). Bare
734
+ * `copy-node` copies the nearest ancestor marked `[data-copy-source]`; a value
735
+ * is a CSS selector resolved with `closest()`. Presence-based for the boolean
736
+ * form. */
737
+ 'copy-node'?: boolean | string;
738
+ /** Lazy copy: with an empty `copy`, activation dispatches `copyrequest` and the
739
+ * write completes once `copy` is set back (within the activation window).
740
+ * Presence-based. */
741
+ 'copy-lazy'?: boolean | '';
742
+ /** Fired after a copy attempt (bubbling `copydone`, `detail: { ok }`).
743
+ * All-lowercase so it binds in React *and* Preact. */
744
+ oncopydone?: (e: CustomEvent<{
745
+ ok: boolean;
746
+ }>) => void;
747
+ /** Fired on a lazy copy activation to request the content (bubbling
748
+ * `copyrequest`). Answer by setting the `copy` attribute. All-lowercase. */
749
+ oncopyrequest?: (e: CustomEvent) => void;
730
750
  }
731
751
  /**
732
752
  * Attributes for the `<a-menu-group>` styled element. For the typed JSX
@@ -770,6 +790,30 @@ export interface AButtonAttributes extends BaseAttributes {
770
790
  form?: string;
771
791
  /** Custom event name dispatched (bubbling) on click. */
772
792
  'data-custom-event'?: string;
793
+ /** Text copied to the clipboard on activation (the "copy button"). The element
794
+ * performs the write itself and flashes a success / failure state. */
795
+ copy?: string;
796
+ /** Copy a DOM node instead of a string (rich `text/html` + plain text). Bare
797
+ * `copy-node` copies the nearest ancestor marked `[data-copy-source]`; a value
798
+ * is a CSS selector resolved with `closest()` (an ancestor region). Presence-
799
+ * based for the boolean form. */
800
+ 'copy-node'?: boolean | string;
801
+ /** Lazy copy: activation dispatches `copyrequest`; the consumer calls
802
+ * `detail.provide(text)` with the content, which stays out of the DOM until
803
+ * then. Takes precedence over `copy`. Presence-based. */
804
+ 'copy-lazy'?: boolean | '';
805
+ /** Fired after a copy attempt (`copydone`, `detail: { ok }`, non-bubbling so a
806
+ * nested copy row can't flip an ancestor's feedback). All-lowercase so it
807
+ * binds in React *and* Preact. */
808
+ oncopydone?: (e: CustomEvent<{
809
+ ok: boolean;
810
+ }>) => void;
811
+ /** Fired on a lazy copy activation (`copyrequest`, non-bubbling). Supply the
812
+ * content by calling `detail.provide(text)` — synchronously or within the
813
+ * click's activation window. All-lowercase. */
814
+ oncopyrequest?: (e: CustomEvent<{
815
+ provide: (text: string) => void;
816
+ }>) => void;
773
817
  'aria-disabled'?: 'true' | 'false' | boolean;
774
818
  'aria-busy'?: 'true' | 'false' | boolean;
775
819
  }
package/dist/index.d.ts CHANGED
@@ -27,7 +27,9 @@ export type { TagProps } from './components/Tag';
27
27
  export { Icon } from './components/Icon';
28
28
  export type { IconProps } from './components/Icon';
29
29
  export { Button } from './components/Button';
30
- export type { ButtonProps, BaseButtonProps, ContentMode, SubmitMode, PriorityMode, } from './components/Button';
30
+ export type { ButtonProps, BaseButtonProps, ContentMode, SubmitMode, PriorityMode, CopyMode, } from './components/Button';
31
+ export { ButtonCopy } from './components/ButtonCopy';
32
+ export type { ButtonCopyProps } from './components/ButtonCopy';
31
33
  export { ICON_SHAPES, ICON_SYNONYMS } from './elements/a-icon.shapes';
32
34
  export { Tooltip } from './components/Tooltip';
33
35
  export type { TooltipProps } from './components/Tooltip';
@@ -37,6 +39,8 @@ export { Menu } from './components/Menu';
37
39
  export type { MenuProps } from './components/Menu';
38
40
  export { MenuItem } from './components/MenuItem';
39
41
  export type { MenuItemProps } from './components/MenuItem';
42
+ export { MenuItemCopy } from './components/MenuItemCopy';
43
+ export type { MenuItemCopyProps } from './components/MenuItemCopy';
40
44
  export { MenuSeparator } from './components/MenuSeparator';
41
45
  export type { MenuSeparatorProps } from './components/MenuSeparator';
42
46
  export { MenuGroup } from './components/MenuGroup';
package/dist/index.js CHANGED
@@ -4,11 +4,13 @@ import { Title } from "./components/Title";
4
4
  import { Tag } from "./components/Tag";
5
5
  import { Icon } from "./components/Icon";
6
6
  import { Button } from "./components/Button";
7
+ import { ButtonCopy } from "./components/ButtonCopy";
7
8
  import { ICON_SHAPES, ICON_SYNONYMS } from "./elements/a-icon.shapes";
8
9
  import { Tooltip } from "./components/Tooltip";
9
10
  import { Checkbox } from "./components/Checkbox";
10
11
  import { Menu } from "./components/Menu";
11
12
  import { MenuItem } from "./components/MenuItem";
13
+ import { MenuItemCopy } from "./components/MenuItemCopy";
12
14
  import { MenuSeparator } from "./components/MenuSeparator";
13
15
  import { MenuGroup } from "./components/MenuGroup";
14
16
  import { Expander } from "./components/Expander";
@@ -43,6 +45,7 @@ import { Card } from "./components/Card";
43
45
  import { configure } from "./jsx-runtime";
44
46
  export {
45
47
  Button,
48
+ ButtonCopy,
46
49
  Calendar,
47
50
  Card,
48
51
  Checkbox,
@@ -58,6 +61,7 @@ export {
58
61
  Menu,
59
62
  MenuGroup,
60
63
  MenuItem,
64
+ MenuItemCopy,
61
65
  MenuSeparator,
62
66
  Progress,
63
67
  RadioGroup,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antadesign/anta",
3
- "version": "0.3.8",
3
+ "version": "0.3.10",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",