@lanfermann/ui 0.3.0 → 0.5.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.
Files changed (81) hide show
  1. package/dist/components/anchor-heading/anchor-heading.d.ts +1 -1
  2. package/dist/components/breadcrumbs/breadcrumbs.js +5 -10
  3. package/dist/components/button/button.d.ts +3 -0
  4. package/dist/components/button/button.js +14 -4
  5. package/dist/components/button-group/button-group-context.js +14 -0
  6. package/dist/components/button-group/button-group-variants.d.ts +14 -0
  7. package/dist/components/button-group/button-group-variants.js +93 -0
  8. package/dist/components/button-group/button-group.d.ts +36 -0
  9. package/dist/components/button-group/button-group.js +46 -0
  10. package/dist/components/code-block/code-block-frame.js +22 -6
  11. package/dist/components/code-block/code-block-live.d.ts +15 -3
  12. package/dist/components/code-block/code-block-live.js +14 -6
  13. package/dist/components/code-block/code-block.d.ts +8 -3
  14. package/dist/components/code-block/code-block.js +9 -3
  15. package/dist/components/code-block/code-keys.d.ts +24 -0
  16. package/dist/components/code-block/code-keys.js +107 -0
  17. package/dist/components/code-block/code-lines-live.d.ts +7 -0
  18. package/dist/components/code-block/code-lines-live.js +167 -0
  19. package/dist/components/code-block/code-lines-view.d.ts +1 -0
  20. package/dist/components/code-block/code-lines-view.js +50 -18
  21. package/dist/components/code-block/code-lines.d.ts +3 -0
  22. package/dist/components/code-block/code-scroll.js +2 -1
  23. package/dist/components/code-block/code-tabs-frame.js +12 -5
  24. package/dist/components/code-block/code-tabs.d.ts +10 -4
  25. package/dist/components/code-block/code-tabs.js +9 -6
  26. package/dist/components/code-block/highlight.d.ts +1 -0
  27. package/dist/components/code-block/line-diff.js +135 -0
  28. package/dist/components/code-block/lines.js +23 -1
  29. package/dist/components/copy-page/copy-page.d.ts +4 -4
  30. package/dist/components/copy-page/copy-page.js +16 -28
  31. package/dist/components/docs-layout/docs-layout.d.ts +1 -1
  32. package/dist/components/docs-layout/docs-layout.js +3 -3
  33. package/dist/components/docs-layout/docs-nav.d.ts +51 -0
  34. package/dist/components/docs-layout/docs-nav.js +60 -0
  35. package/dist/components/grow/grow.d.ts +20 -0
  36. package/dist/components/grow/grow.js +104 -0
  37. package/dist/components/heading/heading.d.ts +4 -0
  38. package/dist/components/heading/heading.js +8 -4
  39. package/dist/components/hover-card/hover-card.d.ts +24 -0
  40. package/dist/components/hover-card/hover-card.js +51 -0
  41. package/dist/components/pager/pager.js +19 -24
  42. package/dist/components/scroll-fade/scroll-fade.d.ts +29 -0
  43. package/dist/components/scroll-fade/scroll-fade.js +40 -0
  44. package/dist/components/scroll-fade/use-scroll-edges.d.ts +13 -0
  45. package/dist/{internal/scroll-edges.js → components/scroll-fade/use-scroll-edges.js} +10 -7
  46. package/dist/components/section/section.d.ts +2 -1
  47. package/dist/components/section/section.js +2 -1
  48. package/dist/components/segmented-control/segmented-control.d.ts +7 -1
  49. package/dist/components/segmented-control/segmented-control.js +22 -11
  50. package/dist/components/select/select.d.ts +4 -1
  51. package/dist/components/select/select.js +15 -3
  52. package/dist/components/shimmer/shimmer.d.ts +21 -0
  53. package/dist/components/shimmer/shimmer.js +24 -0
  54. package/dist/components/side-nav/side-nav.js +11 -20
  55. package/dist/components/site-header/site-header.d.ts +1 -1
  56. package/dist/components/site-header/site-header.js +6 -3
  57. package/dist/components/slider/slider.d.ts +3 -1
  58. package/dist/components/slider/slider.js +45 -19
  59. package/dist/components/table/table.js +2 -1
  60. package/dist/components/tabs/tabs.js +3 -2
  61. package/dist/components/theme-switch/theme-switch.d.ts +47 -0
  62. package/dist/components/theme-switch/theme-switch.js +102 -0
  63. package/dist/components/trace/trace.d.ts +71 -0
  64. package/dist/components/trace/trace.js +201 -0
  65. package/dist/index.d.ts +11 -2
  66. package/dist/index.js +10 -2
  67. package/dist/internal/render-as.js +24 -0
  68. package/dist/internal/rule.js +5 -4
  69. package/package.json +1 -1
  70. package/src/styles/button-group.css +57 -0
  71. package/src/styles/code.css +102 -38
  72. package/src/styles/marks.css +10 -0
  73. package/src/styles/motion.css +4 -3
  74. package/src/styles/scroll-fade.css +26 -20
  75. package/src/styles/shimmer.css +68 -0
  76. package/src/styles/slider.css +57 -19
  77. package/src/styles.css +2 -1
  78. package/dist/components/page-header/page-header.d.ts +0 -30
  79. package/dist/components/page-header/page-header.js +0 -49
  80. package/dist/internal/scroll-fade.js +0 -24
  81. package/src/styles/tabs.css +0 -39
@@ -2,7 +2,7 @@ import { HeadingProps } from "../heading/heading.js";
2
2
  export type AnchorHeadingProps = Omit<HeadingProps, "id" | "level"> & {
3
3
  /** The section's id: the part of the address after `#`. */
4
4
  id: string;
5
- /** `<h2>` to `<h4>`: a section of a page and its parts. The page's title is `PageHeaderTitle`. */
5
+ /** `<h2>` to `<h4>`: a section of a page and its parts. The page's title is a `Heading`. */
6
6
  level?: 2 | 3 | 4;
7
7
  /** The name of the link to the section, read out by a screen reader. */
8
8
  anchorLabel?: string;
@@ -1,8 +1,7 @@
1
- "use client";
2
1
  import { ChevronRightGlyph } from "../../internal/glyphs.js";
2
+ import { renderAs } from "../../internal/render-as.js";
3
3
  import { cn } from "../../lib/cn.js";
4
4
  import { jsx, jsxs } from "react/jsx-runtime";
5
- import { useRender } from "@base-ui/react/use-render";
6
5
  // src/components/breadcrumbs/breadcrumbs.tsx
7
6
  /**
8
7
  * Where a page stands: the places above it, from the outside in, each a step back. It stands
@@ -41,14 +40,10 @@ function BreadcrumbItem({ className, children, ...props }) {
41
40
  }
42
41
  /** A place that is a page. For a router's link, pass it as `render`. */
43
42
  function BreadcrumbLink({ render, className, ...props }) {
44
- return useRender({
45
- defaultTagName: "a",
46
- render,
47
- props: {
48
- ...props,
49
- "data-slot": "breadcrumb-link",
50
- className: cn("rounded-xs underline decoration-transparent decoration-1 underline-offset-[0.25em] transition-colors spring-snap hover:text-fg hover:decoration-current", "focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-accent", className)
51
- }
43
+ return renderAs("a", render, {
44
+ ...props,
45
+ "data-slot": "breadcrumb-link",
46
+ className: cn("rounded-xs underline decoration-transparent decoration-1 underline-offset-[0.25em] transition-colors spring-snap hover:text-fg hover:decoration-current", "focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-accent", className)
52
47
  });
53
48
  }
54
49
  /** The page the reader is on, at the end of the trail. */
@@ -21,5 +21,8 @@ export type ButtonProps = Button$1.Props & Pick<ButtonVariants, "variant" | "siz
21
21
  * Label it with what happens ("Angebot senden", not "OK"). Icons go inside as children, before
22
22
  * the label: 14px in `sm`, 16px in `md`, 18px in `lg`. For a link that should look like a button,
23
23
  * style an `<a>` with `buttonVariants()` instead of rendering one through `Button`.
24
+ *
25
+ * Inside a `ButtonGroup` it is one key of the group's fitting: it has no face of its own and
26
+ * takes the group's size, and its `variant` is not read.
24
27
  */
25
28
  export declare function Button({ className, variant, size, iconOnly, loading, disabled, focusableWhenDisabled, children, ...props }: ButtonProps): import("react").JSX.Element;
@@ -1,4 +1,6 @@
1
1
  "use client";
2
+ import { useButtonGroup } from "../button-group/button-group-context.js";
3
+ import { buttonGroupKeyVariants } from "../button-group/button-group-variants.js";
2
4
  import { Spinner } from "../spinner/spinner.js";
3
5
  import { mergeClassName } from "../../lib/cn.js";
4
6
  import { buttonDisabledLook, buttonVariants } from "./button-variants.js";
@@ -18,19 +20,27 @@ const spinnerSize = {
18
20
  * Label it with what happens ("Angebot senden", not "OK"). Icons go inside as children, before
19
21
  * the label: 14px in `sm`, 16px in `md`, 18px in `lg`. For a link that should look like a button,
20
22
  * style an `<a>` with `buttonVariants()` instead of rendering one through `Button`.
23
+ *
24
+ * Inside a `ButtonGroup` it is one key of the group's fitting: it has no face of its own and
25
+ * takes the group's size, and its `variant` is not read.
21
26
  */
22
27
  function Button({ className, variant, size, iconOnly, loading = false, disabled, focusableWhenDisabled, children, ...props }) {
28
+ const group = useButtonGroup();
23
29
  const resolvedVariant = variant ?? "secondary";
24
- const resolvedSize = size ?? "md";
30
+ const resolvedSize = group?.size ?? size ?? "md";
25
31
  return /* @__PURE__ */ jsx(Button$1, {
26
32
  "data-slot": "button",
27
- "data-variant": resolvedVariant,
33
+ "data-group-key": group ? "" : void 0,
34
+ "data-variant": group ? void 0 : resolvedVariant,
28
35
  "data-size": resolvedSize,
29
36
  "data-loading": loading ? "" : void 0,
30
37
  "aria-busy": loading || void 0,
31
- disabled: disabled || loading,
38
+ disabled: disabled || loading || group?.disabled,
32
39
  focusableWhenDisabled: focusableWhenDisabled ?? loading,
33
- className: mergeClassName([buttonVariants({
40
+ className: mergeClassName(group ? [buttonGroupKeyVariants({
41
+ size: resolvedSize,
42
+ iconOnly
43
+ }), !loading && "data-disabled:bg-disabled data-disabled:text-on-disabled"] : [buttonVariants({
34
44
  variant,
35
45
  size,
36
46
  iconOnly
@@ -0,0 +1,14 @@
1
+ "use client";
2
+ import { createContext, useContext } from "react";
3
+ // src/components/button-group/button-group-context.ts
4
+ /**
5
+ * What a `ButtonGroup` tells the parts inside it. A `Button` or a `SelectTrigger` that finds it
6
+ * is a key of the group's fitting: it gives up its own face, edge and shadow and takes the
7
+ * group's size.
8
+ */
9
+ const ButtonGroupContext = createContext(void 0);
10
+ /** The group a part stands in, or `undefined` outside one. */
11
+ function useButtonGroup() {
12
+ return useContext(ButtonGroupContext);
13
+ }
14
+ export { ButtonGroupContext, useButtonGroup };
@@ -0,0 +1,14 @@
1
+ import { VariantProps } from "class-variance-authority";
2
+ /**
3
+ * The fitting of a `ButtonGroup`: the face, the edge and the shadow that all its keys share. It
4
+ * moves as one part: under the pointer the whole fitting lifts, under a press it goes down and
5
+ * drops its ambient shadow, whichever key it is. Server-safe, like `buttonVariants`.
6
+ *
7
+ * `--key-hover` and `--key-pressed` are what a key lays over the face (button-group.css has the
8
+ * seams and the corners).
9
+ */
10
+ export declare const buttonGroupVariants: (props?: ({
11
+ variant?: "primary" | "secondary" | "ask" | null | undefined;
12
+ size?: "sm" | "md" | "lg" | null | undefined;
13
+ } & import("class-variance-authority/types").ClassProp) | undefined) => string;
14
+ export type ButtonGroupVariants = VariantProps<typeof buttonGroupVariants>;
@@ -0,0 +1,93 @@
1
+ import { cva } from "class-variance-authority";
2
+ // src/components/button-group/button-group-variants.ts
3
+ /**
4
+ * The fitting of a `ButtonGroup`: the face, the edge and the shadow that all its keys share. It
5
+ * moves as one part: under the pointer the whole fitting lifts, under a press it goes down and
6
+ * drops its ambient shadow, whichever key it is. Server-safe, like `buttonVariants`.
7
+ *
8
+ * `--key-hover` and `--key-pressed` are what a key lays over the face (button-group.css has the
9
+ * seams and the corners).
10
+ */
11
+ const buttonGroupVariants = cva([
12
+ "relative inline-flex shrink-0 items-stretch font-medium",
13
+ "transition-[box-shadow,scale] spring-seat",
14
+ "has-[[data-group-key]:hover]:spring-snap has-[[data-group-key]:hover:not(:active)]:depth-raised-hover",
15
+ "has-[[data-group-key]:active]:scale-(--motion-press-scale) has-[[data-group-key]:active]:shadow-pressed has-[[data-group-key]:active]:spring-snap",
16
+ "data-[orientation=vertical]:flex-col",
17
+ "data-disabled:bg-disabled data-disabled:depth-flat"
18
+ ], {
19
+ variants: {
20
+ variant: {
21
+ /** The neutral fitting. The default. */
22
+ secondary: "bg-raised depth-raised text-fg [--key-hover:var(--color-hover)] [--key-pressed:var(--color-raised-pressed)]",
23
+ /** The committed action with a second way beside it: the dark key, split. */
24
+ primary: "bg-key depth-raised text-on-key depth-tone-key [--key-hover:color-mix(in_srgb,currentColor_9%,transparent)] [--key-pressed:var(--color-key-pressed)]",
25
+ /** The ask, split. Indigo, at most once per view. */
26
+ ask: "bg-accent depth-raised text-on-accent depth-tone-accent [--key-hover:color-mix(in_srgb,currentColor_12%,transparent)] [--key-pressed:var(--color-accent-pressed)]"
27
+ },
28
+ size: {
29
+ sm: "rounded-sm text-sm",
30
+ md: "rounded-md text-control",
31
+ lg: "rounded-lg text-control"
32
+ }
33
+ },
34
+ defaultVariants: {
35
+ variant: "secondary",
36
+ size: "md"
37
+ }
38
+ });
39
+ /**
40
+ * A key that cannot be pressed: a flat patch in the fitting, its label still readable. Left out
41
+ * while a key is loading, which is disabled too but keeps its colour.
42
+ */
43
+ const buttonGroupKeyDisabledLook = "data-disabled:bg-disabled data-disabled:text-on-disabled";
44
+ /**
45
+ * One key of the fitting. It has no face of its own: the fitting's gloss shows through, a wash
46
+ * marks the key under the pointer, and a press shades only this key, so the fitting rocks on it
47
+ * while the others keep their gloss. On a touch screen its target grows to 44px up and down,
48
+ * never sideways, so two keys never take each other's taps.
49
+ */
50
+ const buttonGroupKeyVariants = cva([
51
+ "relative inline-flex shrink-0 items-center justify-center whitespace-nowrap outline-offset-2 select-none",
52
+ "transition-[background-color,box-shadow] spring-seat hover:bg-(--key-hover) hover:spring-snap",
53
+ "active:bg-(--key-pressed) active:inset-shadow-pressed active:spring-snap",
54
+ "data-popup-open:bg-(--key-hover)",
55
+ "focus-visible:z-1 focus-visible:rounded-[inherit] focus-visible:outline-2 focus-visible:outline-accent",
56
+ "pointer-coarse:after:absolute pointer-coarse:after:inset-x-0 pointer-coarse:after:top-1/2 pointer-coarse:after:h-11 pointer-coarse:after:-translate-y-1/2 pointer-coarse:after:content-['']",
57
+ "data-disabled:pointer-events-none"
58
+ ], {
59
+ variants: {
60
+ size: {
61
+ sm: "h-(--control-sm) gap-1.5 px-(--control-pad-sm)",
62
+ md: "h-(--control-md) gap-2 px-(--control-pad-md)",
63
+ lg: "h-(--control-lg) gap-2 px-(--control-pad-lg)"
64
+ },
65
+ /** A square key that holds one icon. */
66
+ iconOnly: {
67
+ true: "px-0",
68
+ false: ""
69
+ }
70
+ },
71
+ compoundVariants: [
72
+ {
73
+ iconOnly: true,
74
+ size: "sm",
75
+ class: "w-(--control-sm)"
76
+ },
77
+ {
78
+ iconOnly: true,
79
+ size: "md",
80
+ class: "w-(--control-md)"
81
+ },
82
+ {
83
+ iconOnly: true,
84
+ size: "lg",
85
+ class: "w-(--control-lg)"
86
+ }
87
+ ],
88
+ defaultVariants: {
89
+ size: "md",
90
+ iconOnly: false
91
+ }
92
+ });
93
+ export { buttonGroupKeyDisabledLook, buttonGroupKeyVariants, buttonGroupVariants };
@@ -0,0 +1,36 @@
1
+ import { ButtonGroupVariants } from "./button-group-variants.js";
2
+ import { ComponentProps } from "react";
3
+ /** A group is a set of actions, and a set needs a name. */
4
+ type Named = {
5
+ "aria-label": string;
6
+ "aria-labelledby"?: undefined;
7
+ } | {
8
+ "aria-labelledby": string;
9
+ "aria-label"?: undefined;
10
+ };
11
+ export type ButtonGroupProps = Omit<ComponentProps<"div">, "aria-label" | "aria-labelledby"> & Named & Pick<ButtonGroupVariants, "variant"> & {
12
+ /** The size of every key in it. */
13
+ size?: "sm" | "md" | "lg";
14
+ /** In a row (the default) or one over the other. */
15
+ orientation?: "horizontal" | "vertical";
16
+ /** None of its keys can be pressed, and the fitting lies flat. */
17
+ disabled?: boolean;
18
+ };
19
+ /**
20
+ * Actions that belong together, as one fitting with several keys: an action and the menu of its
21
+ * other forms ("Senden" and a chevron), two or three steps on the same thing ("Archivieren",
22
+ * "Melden", "Später"), a pair of arrows. The keys share one face, one edge and one shadow, a
23
+ * seam in the edge's colour divides them, and the fitting moves as one part: it lifts under the
24
+ * pointer and goes down under a press, and only the pressed key takes the inner shade, so it
25
+ * rocks on that key.
26
+ *
27
+ * Put `Button`s inside, or a `SelectTrigger`. A key that opens a popup is its trigger rendered
28
+ * as a button (`<MenuTrigger render={<Button iconOnly aria-label="…" />}>`). The keys take the
29
+ * group's `variant` and `size`; their own are not read. Keep the keys direct children.
30
+ *
31
+ * It is for actions. A choice between views, with one of them selected, is a
32
+ * `SegmentedControl`; a field with something beside its text is an `InputGroup`. Name the group
33
+ * (`aria-label`): Tab goes from key to key, and a screen reader says the name on the way in.
34
+ */
35
+ export declare function ButtonGroup({ variant, size, orientation, disabled, className, ...props }: ButtonGroupProps): import("react").JSX.Element;
36
+ export {};
@@ -0,0 +1,46 @@
1
+ "use client";
2
+ import { cn } from "../../lib/cn.js";
3
+ import { ButtonGroupContext } from "./button-group-context.js";
4
+ import { buttonGroupVariants } from "./button-group-variants.js";
5
+ import { jsx } from "react/jsx-runtime";
6
+ import { useMemo } from "react";
7
+ // src/components/button-group/button-group.tsx
8
+ /**
9
+ * Actions that belong together, as one fitting with several keys: an action and the menu of its
10
+ * other forms ("Senden" and a chevron), two or three steps on the same thing ("Archivieren",
11
+ * "Melden", "Später"), a pair of arrows. The keys share one face, one edge and one shadow, a
12
+ * seam in the edge's colour divides them, and the fitting moves as one part: it lifts under the
13
+ * pointer and goes down under a press, and only the pressed key takes the inner shade, so it
14
+ * rocks on that key.
15
+ *
16
+ * Put `Button`s inside, or a `SelectTrigger`. A key that opens a popup is its trigger rendered
17
+ * as a button (`<MenuTrigger render={<Button iconOnly aria-label="…" />}>`). The keys take the
18
+ * group's `variant` and `size`; their own are not read. Keep the keys direct children.
19
+ *
20
+ * It is for actions. A choice between views, with one of them selected, is a
21
+ * `SegmentedControl`; a field with something beside its text is an `InputGroup`. Name the group
22
+ * (`aria-label`): Tab goes from key to key, and a screen reader says the name on the way in.
23
+ */
24
+ function ButtonGroup({ variant, size = "md", orientation = "horizontal", disabled = false, className, ...props }) {
25
+ const context = useMemo(() => ({
26
+ size,
27
+ disabled
28
+ }), [size, disabled]);
29
+ return /* @__PURE__ */ jsx(ButtonGroupContext, {
30
+ value: context,
31
+ children: /* @__PURE__ */ jsx("div", {
32
+ role: "group",
33
+ "data-slot": "button-group",
34
+ "data-variant": variant ?? "secondary",
35
+ "data-size": size,
36
+ "data-orientation": orientation,
37
+ "data-disabled": disabled ? "" : void 0,
38
+ className: cn(buttonGroupVariants({
39
+ variant,
40
+ size
41
+ }), className),
42
+ ...props
43
+ })
44
+ });
45
+ }
46
+ export { ButtonGroup };
@@ -4,6 +4,7 @@ import { Icon } from "../icon/icon.js";
4
4
  import { ChevronDownGlyph } from "../../internal/glyphs.js";
5
5
  import { cn } from "../../lib/cn.js";
6
6
  import { CodeCopy } from "./code-copy.js";
7
+ import { CodeKeys } from "./code-keys.js";
7
8
  import { CodeScroll } from "./code-scroll.js";
8
9
  import { codeHead, codeRoot } from "./code-surface.js";
9
10
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
@@ -13,15 +14,29 @@ import { useId, useLayoutEffect, useRef, useState } from "react";
13
14
  const PAD_Y_REM = .75;
14
15
  const LINE_REM = 1.375;
15
16
  /**
16
- * The client half of a CodeBlock: its header, the scrolling region, the copy button and the
17
- * fold. The code itself arrives finished as `children`.
17
+ * The client half of a CodeBlock: its header, the scrolling region, its keys (the copy, and on
18
+ * request a download and a line wrap) and the fold. The code itself arrives finished as
19
+ * `children`.
20
+ *
21
+ * With a wrap key the block says whether lines break (`data-wrap` is `on` or `off`) and the
22
+ * stylesheet follows it, whatever the code was rendered with: the code stays the server's.
18
23
  */
19
- function CodeBlockFrame({ children, copyText, lineCount, title, icon, mark, prompt, label, fold, foldLabel, className, ...labels }) {
24
+ function CodeBlockFrame({ children, copyText, lineCount, fileName, wrap = false, wrapToggle = false, title, icon, mark, prompt, label, fold, foldLabel, className, ...keyOptions }) {
20
25
  const codeRef = useRef(null);
26
+ const [wrapped, setWrapped] = useState(wrap);
21
27
  const copy = /* @__PURE__ */ jsx(CodeCopy, {
22
28
  text: copyText,
23
29
  codeRef,
24
- ...labels
30
+ ...keyOptions
31
+ });
32
+ const keys = /* @__PURE__ */ jsx(CodeKeys, {
33
+ text: copyText,
34
+ codeRef,
35
+ fileName,
36
+ wrapToggle,
37
+ wrapped,
38
+ onWrappedChange: setWrapped,
39
+ ...keyOptions
25
40
  });
26
41
  if (prompt) return /* @__PURE__ */ jsxs("div", {
27
42
  "data-slot": "code-block",
@@ -44,6 +59,7 @@ function CodeBlockFrame({ children, copyText, lineCount, title, icon, mark, prom
44
59
  });
45
60
  return /* @__PURE__ */ jsxs("div", {
46
61
  "data-slot": "code-block",
62
+ "data-wrap": wrapToggle ? wrapped ? "on" : "off" : void 0,
47
63
  className: cn(codeRoot, className),
48
64
  children: [title ? /* @__PURE__ */ jsxs("div", {
49
65
  "data-slot": "code-block-header",
@@ -58,11 +74,11 @@ function CodeBlockFrame({ children, copyText, lineCount, title, icon, mark, prom
58
74
  className: "min-w-0 flex-1 truncate font-mono text-fg-body",
59
75
  children: title
60
76
  }),
61
- copy
77
+ keys
62
78
  ]
63
79
  }) : /* @__PURE__ */ jsx("div", {
64
80
  className: "absolute top-1.5 right-1.5 z-10 rounded-md bg-(--code-face)",
65
- children: copy
81
+ children: keys
66
82
  }), /* @__PURE__ */ jsx(CodeFold, {
67
83
  lineCount,
68
84
  fold,
@@ -1,5 +1,6 @@
1
1
  import { IconSvgElement } from "../icon/icon.js";
2
- import { CodeCopyLabels } from "./code-copy.js";
2
+ import { CodeKeyOptions } from "./code-keys.js";
3
+ import { CodeChangeMotion } from "./code-lines-live.js";
3
4
  import { CodeLinesOptions } from "./code-lines-view.js";
4
5
  import { ReactNode } from "react";
5
6
  /**
@@ -12,7 +13,7 @@ export type CodePiece = {
12
13
  text: string;
13
14
  role?: CodeRole;
14
15
  };
15
- export type CodeBlockLiveProps = CodeLinesOptions & CodeCopyLabels & {
16
+ export type CodeBlockLiveProps = CodeLinesOptions & CodeKeyOptions & {
16
17
  /** The code, line by line, each line as its runs in order. */
17
18
  lines: readonly (readonly CodePiece[])[];
18
19
  /** The file name or the place the code goes, set in the header. Without it, no header. */
@@ -29,6 +30,12 @@ export type CodeBlockLiveProps = CodeLinesOptions & CodeCopyLabels & {
29
30
  fold?: number;
30
31
  foldLabel?: (hidden: number) => string;
31
32
  className?: string;
33
+ /**
34
+ * How what a change put into a line (a value, a comma) shows: it fades in (`fade`, the
35
+ * default), or also lights up in the accent for a moment (`glow`), for a change the reader
36
+ * should not miss.
37
+ */
38
+ changeMotion?: CodeChangeMotion;
32
39
  };
33
40
  /**
34
41
  * A `CodeBlock` for code that is put together in the browser: a configuration generated from the
@@ -41,5 +48,10 @@ export type CodeBlockLiveProps = CodeLinesOptions & CodeCopyLabels & {
41
48
  * along, about a megabyte for a few lines. So: `CodeBlock` for code that is known when the page
42
49
  * is built, `CodeBlockLive` for code that changes under the reader's hand. Mark the lines their
43
50
  * choice put there (`marked`), so they see what it changed.
51
+ *
52
+ * When the code changes, the block shows the change: a line that joins opens like a fold (its
53
+ * row opens and the lines below make room while its text fades in), a line that goes fades and
54
+ * its row closes, and what a change put into a line arrives as new (`changeMotion`). The block's
55
+ * height follows, so a `Grow` around it needs nothing more.
44
56
  */
45
- export declare function CodeBlockLive({ lines, title, icon, mark, label, prompt, fold, foldLabel, className, lineNumbers, marked, added, removed, wrap, addedLabel, removedLabel, ...copyLabels }: CodeBlockLiveProps): import("react").JSX.Element;
57
+ export declare function CodeBlockLive({ lines, title, icon, mark, label, prompt, fold, foldLabel, className, lineNumbers, marked, added, removed, wrap, addedLabel, removedLabel, changeMotion, ...keyOptions }: CodeBlockLiveProps): import("react").JSX.Element;
@@ -1,6 +1,6 @@
1
1
  import { CodeBlockFrame } from "./code-block-frame.js";
2
- import { CodeLinesView } from "./code-lines-view.js";
3
- import { parseLines } from "./lines.js";
2
+ import { CodeLinesLive } from "./code-lines-live.js";
3
+ import { codeFileName, parseLines } from "./lines.js";
4
4
  import { jsx } from "react/jsx-runtime";
5
5
  import { FileCodeIcon } from "@hugeicons/core-free-icons";
6
6
  // src/components/code-block/code-block-live.tsx
@@ -15,8 +15,13 @@ import { FileCodeIcon } from "@hugeicons/core-free-icons";
15
15
  * along, about a megabyte for a few lines. So: `CodeBlock` for code that is known when the page
16
16
  * is built, `CodeBlockLive` for code that changes under the reader's hand. Mark the lines their
17
17
  * choice put there (`marked`), so they see what it changed.
18
+ *
19
+ * When the code changes, the block shows the change: a line that joins opens like a fold (its
20
+ * row opens and the lines below make room while its text fades in), a line that goes fades and
21
+ * its row closes, and what a change put into a line arrives as new (`changeMotion`). The block's
22
+ * height follows, so a `Grow` around it needs nothing more.
18
23
  */
19
- function CodeBlockLive({ lines, title, icon = FileCodeIcon, mark, label = "Code", prompt = false, fold, foldLabel = (hidden) => `${hidden} weitere Zeilen`, className, lineNumbers, marked, added, removed, wrap, addedLabel, removedLabel, ...copyLabels }) {
24
+ function CodeBlockLive({ lines, title, icon = FileCodeIcon, mark, label = "Code", prompt = false, fold, foldLabel = (hidden) => `${hidden} weitere Zeilen`, className, lineNumbers, marked, added, removed, wrap, addedLabel, removedLabel, changeMotion, ...keyOptions }) {
20
25
  const coloured = lines.map((line) => line.map((piece) => ({
21
26
  content: piece.text,
22
27
  color: `var(--lf-code-${piece.role ?? "text"})`,
@@ -35,8 +40,10 @@ function CodeBlockLive({ lines, title, icon = FileCodeIcon, mark, label = "Code"
35
40
  fold,
36
41
  foldLabel: fold === void 0 ? void 0 : foldLabel(lines.length - fold),
37
42
  className,
38
- ...copyLabels,
39
- children: /* @__PURE__ */ jsx(CodeLinesView, {
43
+ fileName: codeFileName(title),
44
+ wrap,
45
+ ...keyOptions,
46
+ children: /* @__PURE__ */ jsx(CodeLinesLive, {
40
47
  lines: coloured,
41
48
  lineNumbers,
42
49
  marked,
@@ -44,7 +51,8 @@ function CodeBlockLive({ lines, title, icon = FileCodeIcon, mark, label = "Code"
44
51
  removed,
45
52
  wrap,
46
53
  addedLabel,
47
- removedLabel
54
+ removedLabel,
55
+ changeMotion
48
56
  })
49
57
  });
50
58
  }
@@ -1,9 +1,10 @@
1
1
  import { IconSvgElement } from "../icon/icon.js";
2
- import { CodeCopyLabels } from "./code-copy.js";
2
+ import { CodeKeyOptions } from "./code-keys.js";
3
3
  import { CodeLinesOptions } from "./code-lines-view.js";
4
+ import "./code-lines.js";
4
5
  import { CodeLanguage } from "./highlight.js";
5
6
  import { ReactNode } from "react";
6
- export type CodeBlockProps = CodeLinesOptions & CodeCopyLabels & {
7
+ export type CodeBlockProps = CodeLinesOptions & CodeKeyOptions & {
7
8
  /** The code, as it is in the file. One trailing line break is ignored. */
8
9
  code: string;
9
10
  language?: CodeLanguage;
@@ -32,5 +33,9 @@ export type CodeBlockProps = CodeLinesOptions & CodeCopyLabels & {
32
33
  *
33
34
  * Mark the lines a text talks about (`marked`), show a change with `added` and `removed`, and
34
35
  * keep the file name as it is (`quote-form.tsx`). For a word in running text, use `Code`.
36
+ *
37
+ * Two more keys can stand beside the copy. `download` saves the code as a file: for a whole file
38
+ * a reader will keep, such as a configuration. `wrapToggle` lets the reader break long lines
39
+ * and put them back: for code with long lines that are read, not run.
35
40
  */
36
- export declare function CodeBlock({ code, language, title, icon, mark, label, fold, foldLabel, className, lineNumbers, marked, added, removed, wrap, addedLabel, removedLabel, ...copyLabels }: CodeBlockProps): import("react").JSX.Element;
41
+ export declare function CodeBlock({ code, language, title, icon, mark, label, fold, foldLabel, className, lineNumbers, marked, added, removed, wrap, addedLabel, removedLabel, ...keyOptions }: CodeBlockProps): import("react").JSX.Element;
@@ -1,7 +1,7 @@
1
1
  import { CodeBlockFrame } from "./code-block-frame.js";
2
2
  import { CodeLines } from "./code-lines.js";
3
3
  import { isShell } from "./highlight.js";
4
- import { copyText, trimCode } from "./lines.js";
4
+ import { codeFileName, copyText, trimCode } from "./lines.js";
5
5
  import { jsx } from "react/jsx-runtime";
6
6
  import { CommandLineIcon, FileBracesIcon, FileCodeIcon } from "@hugeicons/core-free-icons";
7
7
  // src/components/code-block/code-block.tsx
@@ -19,8 +19,12 @@ function codeSymbol(language) {
19
19
  *
20
20
  * Mark the lines a text talks about (`marked`), show a change with `added` and `removed`, and
21
21
  * keep the file name as it is (`quote-form.tsx`). For a word in running text, use `Code`.
22
+ *
23
+ * Two more keys can stand beside the copy. `download` saves the code as a file: for a whole file
24
+ * a reader will keep, such as a configuration. `wrapToggle` lets the reader break long lines
25
+ * and put them back: for code with long lines that are read, not run.
22
26
  */
23
- function CodeBlock({ code, language = "text", title, icon, mark, label = "Code", fold, foldLabel = (hidden) => `${hidden} weitere Zeilen`, className, lineNumbers, marked, added, removed, wrap, addedLabel, removedLabel, ...copyLabels }) {
27
+ function CodeBlock({ code, language = "text", title, icon, mark, label = "Code", fold, foldLabel = (hidden) => `${hidden} weitere Zeilen`, className, lineNumbers, marked, added, removed, wrap, addedLabel, removedLabel, ...keyOptions }) {
24
28
  const lineCount = trimCode(code).split("\n").length;
25
29
  const prompt = isShell(language) && lineCount === 1 && !title;
26
30
  return /* @__PURE__ */ jsx(CodeBlockFrame, {
@@ -34,7 +38,9 @@ function CodeBlock({ code, language = "text", title, icon, mark, label = "Code",
34
38
  fold,
35
39
  foldLabel: fold === void 0 ? void 0 : foldLabel(lineCount - fold),
36
40
  className,
37
- ...copyLabels,
41
+ fileName: codeFileName(title, language),
42
+ wrap,
43
+ ...keyOptions,
38
44
  children: /* @__PURE__ */ jsx(CodeLines, {
39
45
  code,
40
46
  language,
@@ -0,0 +1,24 @@
1
+ import { CodeCopyLabels } from "./code-copy.js";
2
+ import "react";
3
+ /** The two keys a block of code may carry beside its copy, and their words. */
4
+ export type CodeKeyOptions = CodeCopyLabels & {
5
+ /**
6
+ * Adds a key that saves the code as a file: for a whole file a reader will keep (a
7
+ * configuration, a script), not for a snippet. `true` names the file after the block's title;
8
+ * a string is the file's name.
9
+ */
10
+ download?: boolean | string;
11
+ /** The key's name and its tooltip. */
12
+ downloadLabel?: string;
13
+ /** The tooltip once the file is on its way. */
14
+ downloadedLabel?: string;
15
+ /** Read out once: a full sentence. */
16
+ downloadedMessage?: string;
17
+ /**
18
+ * Adds a key that lets the reader break long lines instead of scrolling them sideways, and
19
+ * back. `wrap` says how the block starts.
20
+ */
21
+ wrapToggle?: boolean;
22
+ /** The key's name and its tooltip. It is a toggle: pressed while lines break. */
23
+ wrapLabel?: string;
24
+ };
@@ -0,0 +1,107 @@
1
+ "use client";
2
+ import { Button } from "../button/button.js";
3
+ import { Icon } from "../icon/icon.js";
4
+ import { Tooltip, TooltipContent, TooltipTrigger } from "../tooltip/tooltip.js";
5
+ import { TickGlyph } from "../../internal/glyphs.js";
6
+ import { cn } from "../../lib/cn.js";
7
+ import { CodeCopy } from "./code-copy.js";
8
+ import { jsx, jsxs } from "react/jsx-runtime";
9
+ import { Download01Icon, TextWrapIcon } from "@hugeicons/core-free-icons";
10
+ import { useEffect, useRef, useState } from "react";
11
+ // src/components/code-block/code-keys.tsx
12
+ /** How long the tick stands before the symbol comes back (as in `CodeCopy`). */
13
+ const SHOWN_MS = 1600;
14
+ /**
15
+ * The keys of a block of code, at the end of its header or in its top corner: line wrap, the
16
+ * download, and last the copy, which every block has and which so keeps its place.
17
+ */
18
+ function CodeKeys({ text, codeRef, fileName = "code.txt", download, downloadLabel, downloadedLabel, downloadedMessage, wrapToggle, wrapLabel = "Zeilen umbrechen", wrapped, onWrappedChange, className, ...copyLabels }) {
19
+ return /* @__PURE__ */ jsxs("div", {
20
+ "data-slot": "code-keys",
21
+ className: cn("flex items-center gap-0.5", className),
22
+ children: [
23
+ wrapToggle && /* @__PURE__ */ jsxs(Tooltip, { children: [/* @__PURE__ */ jsx(TooltipTrigger, { render: /* @__PURE__ */ jsx(Button, {
24
+ iconOnly: true,
25
+ size: "sm",
26
+ variant: "quiet",
27
+ "aria-label": wrapLabel,
28
+ "aria-pressed": wrapped,
29
+ "data-slot": "code-wrap",
30
+ onClick: () => onWrappedChange(!wrapped),
31
+ className: "text-fg-muted hover:text-fg aria-pressed:bg-hover aria-pressed:text-fg",
32
+ children: /* @__PURE__ */ jsx(Icon, {
33
+ icon: TextWrapIcon,
34
+ size: 14
35
+ })
36
+ }) }), /* @__PURE__ */ jsx(TooltipContent, { children: wrapLabel })] }),
37
+ download !== void 0 && download !== false && /* @__PURE__ */ jsx(CodeDownload, {
38
+ text,
39
+ fileName: typeof download === "string" ? download : fileName,
40
+ downloadLabel,
41
+ downloadedLabel,
42
+ downloadedMessage
43
+ }),
44
+ /* @__PURE__ */ jsx(CodeCopy, {
45
+ text,
46
+ codeRef,
47
+ ...copyLabels
48
+ })
49
+ ]
50
+ });
51
+ }
52
+ /**
53
+ * Saves the code as a file, and confirms like the copy beside it: the symbol gives way to a
54
+ * drawn tick (copy.css) and the tooltip says that it worked.
55
+ */
56
+ function CodeDownload({ text, fileName, downloadLabel = "Herunterladen", downloadedLabel = "Heruntergeladen", downloadedMessage = "Die Datei wurde heruntergeladen." }) {
57
+ const [done, setDone] = useState(false);
58
+ const timer = useRef(void 0);
59
+ useEffect(() => () => clearTimeout(timer.current), []);
60
+ function save() {
61
+ const url = URL.createObjectURL(new Blob([`${text}\n`], { type: "text/plain;charset=utf-8" }));
62
+ const link = document.createElement("a");
63
+ link.href = url;
64
+ link.download = fileName;
65
+ document.body.append(link);
66
+ link.click();
67
+ link.remove();
68
+ setTimeout(() => URL.revokeObjectURL(url), 0);
69
+ clearTimeout(timer.current);
70
+ setDone(true);
71
+ timer.current = setTimeout(() => setDone(false), SHOWN_MS);
72
+ }
73
+ return /* @__PURE__ */ jsxs("div", {
74
+ "data-slot": "code-download",
75
+ className: "relative flex items-center",
76
+ children: [/* @__PURE__ */ jsxs(Tooltip, { children: [/* @__PURE__ */ jsx(TooltipTrigger, {
77
+ closeOnClick: false,
78
+ render: /* @__PURE__ */ jsx(Button, {
79
+ iconOnly: true,
80
+ size: "sm",
81
+ variant: "quiet",
82
+ "aria-label": downloadLabel,
83
+ onClick: save,
84
+ className: "text-fg-muted hover:text-fg",
85
+ children: /* @__PURE__ */ jsxs("span", {
86
+ "data-copied": done ? "" : void 0,
87
+ className: "grid place-items-center *:col-start-1 *:row-start-1",
88
+ children: [/* @__PURE__ */ jsx(Icon, {
89
+ "data-slot": "code-copy-symbol",
90
+ icon: Download01Icon,
91
+ size: 14
92
+ }), /* @__PURE__ */ jsx(TickGlyph, {
93
+ "data-slot": "code-copy-tick",
94
+ width: 14,
95
+ height: 14,
96
+ strokeWidth: 1.5
97
+ })]
98
+ })
99
+ })
100
+ }), /* @__PURE__ */ jsx(TooltipContent, { children: done ? downloadedLabel : downloadLabel })] }), /* @__PURE__ */ jsx("span", {
101
+ role: "status",
102
+ className: "sr-only",
103
+ children: done ? downloadedMessage : ""
104
+ })]
105
+ });
106
+ }
107
+ export { CodeKeys };
@@ -0,0 +1,7 @@
1
+ import "./code-lines-view.js";
2
+ /**
3
+ * How the part of a line that a change put in shows (a value, a comma): it fades in (`fade`),
4
+ * or also lights up in the accent and lets the light go (`glow`), for a change the reader should
5
+ * not miss.
6
+ */
7
+ export type CodeChangeMotion = "fade" | "glow";