@slurp-ui/react 1.1.0 → 1.2.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/CHANGELOG.md +56 -0
- package/dist/Bloom/Bloom.js +3 -2
- package/dist/Bloom/Bloom.js.map +1 -1
- package/dist/Calendar/Calendar.d.ts +42 -0
- package/dist/Calendar/Calendar.js +178 -0
- package/dist/Calendar/Calendar.js.map +1 -0
- package/dist/Calendar/dates.d.ts +3 -0
- package/dist/Calendar/dates.js +46 -0
- package/dist/Calendar/dates.js.map +1 -0
- package/dist/Calendar/index.d.ts +3 -0
- package/dist/Calendar/index.js +2 -0
- package/dist/Card/Card.d.ts +1 -1
- package/dist/Card/Card.js.map +1 -1
- package/dist/Carousel/Carousel.js +2 -0
- package/dist/Carousel/Carousel.js.map +1 -1
- package/dist/Choice/Choice.js +2 -0
- package/dist/Choice/Choice.js.map +1 -1
- package/dist/Code/Code.d.ts +38 -0
- package/dist/Code/Code.js +82 -0
- package/dist/Code/Code.js.map +1 -0
- package/dist/Code/index.d.ts +3 -0
- package/dist/Code/index.js +2 -0
- package/dist/Contents/Contents.d.ts +35 -0
- package/dist/Contents/Contents.js +63 -0
- package/dist/Contents/Contents.js.map +1 -0
- package/dist/Contents/index.d.ts +3 -0
- package/dist/Contents/index.js +2 -0
- package/dist/DateField/DateField.d.ts +29 -0
- package/dist/DateField/DateField.js +118 -0
- package/dist/DateField/DateField.js.map +1 -0
- package/dist/DateField/index.d.ts +3 -0
- package/dist/DateField/index.js +2 -0
- package/dist/Dialog/Dialog.js +73 -25
- package/dist/Dialog/Dialog.js.map +1 -1
- package/dist/Dock/Dock.d.ts +6 -1
- package/dist/Dock/Dock.js +13 -2
- package/dist/Dock/Dock.js.map +1 -1
- package/dist/ErrorSummary/ErrorSummary.d.ts +26 -0
- package/dist/ErrorSummary/ErrorSummary.js +68 -0
- package/dist/ErrorSummary/ErrorSummary.js.map +1 -0
- package/dist/ErrorSummary/index.d.ts +3 -0
- package/dist/ErrorSummary/index.js +2 -0
- package/dist/FileDrop/FileDrop.d.ts +34 -0
- package/dist/FileDrop/FileDrop.js +145 -0
- package/dist/FileDrop/FileDrop.js.map +1 -0
- package/dist/FileDrop/index.d.ts +3 -0
- package/dist/FileDrop/index.js +2 -0
- package/dist/Filters/Filters.js +2 -0
- package/dist/Filters/Filters.js.map +1 -1
- package/dist/Footer/Footer.d.ts +37 -0
- package/dist/Footer/Footer.js +63 -0
- package/dist/Footer/Footer.js.map +1 -0
- package/dist/Footer/index.d.ts +3 -0
- package/dist/Footer/index.js +2 -0
- package/dist/Hairline/Hairline.d.ts +16 -0
- package/dist/Hairline/Hairline.js +23 -0
- package/dist/Hairline/Hairline.js.map +1 -0
- package/dist/Hairline/index.d.ts +3 -0
- package/dist/Hairline/index.js +2 -0
- package/dist/Hint/Hint.js +20 -1
- package/dist/Hint/Hint.js.map +1 -1
- package/dist/Kbd/Kbd.d.ts +7 -0
- package/dist/Kbd/Kbd.js +20 -0
- package/dist/Kbd/Kbd.js.map +1 -0
- package/dist/Kbd/index.d.ts +3 -0
- package/dist/Kbd/index.js +2 -0
- package/dist/ListRow/ListRow.d.ts +6 -2
- package/dist/ListRow/ListRow.js +5 -1
- package/dist/ListRow/ListRow.js.map +1 -1
- package/dist/Media/Media.d.ts +28 -0
- package/dist/Media/Media.js +65 -0
- package/dist/Media/Media.js.map +1 -0
- package/dist/Media/index.d.ts +3 -0
- package/dist/Media/index.js +2 -0
- package/dist/Notice/Notice.js +2 -0
- package/dist/Notice/Notice.js.map +1 -1
- package/dist/NumberField/NumberField.d.ts +32 -0
- package/dist/NumberField/NumberField.js +126 -0
- package/dist/NumberField/NumberField.js.map +1 -0
- package/dist/NumberField/index.d.ts +3 -0
- package/dist/NumberField/index.js +2 -0
- package/dist/PageNav/PageNav.d.ts +25 -0
- package/dist/PageNav/PageNav.js +57 -0
- package/dist/PageNav/PageNav.js.map +1 -0
- package/dist/PageNav/index.d.ts +3 -0
- package/dist/PageNav/index.js +2 -0
- package/dist/Pages/Pages.js +2 -0
- package/dist/Pages/Pages.js.map +1 -1
- package/dist/PinField/PinField.d.ts +26 -0
- package/dist/PinField/PinField.js +73 -0
- package/dist/PinField/PinField.js.map +1 -0
- package/dist/PinField/index.d.ts +3 -0
- package/dist/PinField/index.js +2 -0
- package/dist/Prose/Prose.d.ts +15 -0
- package/dist/Prose/Prose.js +25 -0
- package/dist/Prose/Prose.js.map +1 -0
- package/dist/Prose/index.d.ts +3 -0
- package/dist/Prose/index.js +2 -0
- package/dist/Quote/Quote.d.ts +19 -0
- package/dist/Quote/Quote.js +52 -0
- package/dist/Quote/Quote.js.map +1 -0
- package/dist/Quote/index.d.ts +3 -0
- package/dist/Quote/index.js +2 -0
- package/dist/Rail/Rail.d.ts +37 -0
- package/dist/Rail/Rail.js +61 -0
- package/dist/Rail/Rail.js.map +1 -0
- package/dist/Rail/index.d.ts +3 -0
- package/dist/Rail/index.js +2 -0
- package/dist/Range/Range.d.ts +1 -1
- package/dist/Range/Range.js.map +1 -1
- package/dist/Rating/Rating.js +2 -0
- package/dist/Rating/Rating.js.map +1 -1
- package/dist/Search/Search.d.ts +1 -1
- package/dist/Search/Search.js +2 -0
- package/dist/Search/Search.js.map +1 -1
- package/dist/SecretField/SecretField.d.ts +17 -0
- package/dist/SecretField/SecretField.js +59 -0
- package/dist/SecretField/SecretField.js.map +1 -0
- package/dist/SecretField/index.d.ts +3 -0
- package/dist/SecretField/index.js +2 -0
- package/dist/Settings/Settings.js +2 -0
- package/dist/Settings/Settings.js.map +1 -1
- package/dist/SkipLink/SkipLink.d.ts +22 -0
- package/dist/SkipLink/SkipLink.js +32 -0
- package/dist/SkipLink/SkipLink.js.map +1 -0
- package/dist/SkipLink/index.d.ts +3 -0
- package/dist/SkipLink/index.js +2 -0
- package/dist/SlurpProvider/SlurpProvider.d.ts +7 -1
- package/dist/SlurpProvider/SlurpProvider.js +14 -6
- package/dist/SlurpProvider/SlurpProvider.js.map +1 -1
- package/dist/Steps/Steps.js +2 -0
- package/dist/Steps/Steps.js.map +1 -1
- package/dist/Suggest/Suggest.js +2 -0
- package/dist/Suggest/Suggest.js.map +1 -1
- package/dist/Table/Table.js +2 -0
- package/dist/Table/Table.js.map +1 -1
- package/dist/Tag/Tag.d.ts +14 -0
- package/dist/Tag/Tag.js +26 -0
- package/dist/Tag/Tag.js.map +1 -0
- package/dist/Tag/index.d.ts +3 -0
- package/dist/Tag/index.js +2 -0
- package/dist/Toast/Toast.js +31 -7
- package/dist/Toast/Toast.js.map +1 -1
- package/dist/index.d.ts +45 -4
- package/dist/index.js +23 -1
- package/dist/internal/labels.js +12 -0
- package/dist/internal/labels.js.map +1 -0
- package/dist/lib/labels.d.ts +175 -0
- package/dist/lib/labels.js +116 -0
- package/dist/lib/labels.js.map +1 -0
- package/dist/lib/theme.d.ts +98 -3
- package/dist/lib/theme.js.map +1 -1
- package/dist/styles.css +1435 -127
- package/dist/tokens.css +10 -0
- package/dist/utils/focus.d.ts +1 -1
- package/dist/utils/focus.js +2 -2
- package/dist/utils/focus.js.map +1 -1
- package/dist/utils/index.d.ts +3 -1
- package/dist/utils/index.js +3 -1
- package/dist/utils/useClipboard.d.ts +14 -0
- package/dist/utils/useClipboard.js +30 -0
- package/dist/utils/useClipboard.js.map +1 -0
- package/dist/utils/useScrollSpy.d.ts +14 -0
- package/dist/utils/useScrollSpy.js +27 -0
- package/dist/utils/useScrollSpy.js.map +1 -0
- package/package.json +5 -5
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Choice.js","names":[],"sources":["../../src/Choice/Choice.tsx"],"sourcesContent":["import { CheckIcon, ChevronDownIcon } from '@slurp-ui/icons';\nimport { cx } from '@slurp-ui/utils';\nimport { useId, useRef, type ElementType, type KeyboardEvent, type ReactNode } from 'react';\n\nimport { Bloom, type BloomPlacement } from '../Bloom/Bloom';\nimport { useAnimationRoot } from '../internal/animation';\nimport { useCss, useThemeProps } from '../internal/styled';\nimport type { PolymorphicProps } from '../lib/polymorphic';\nimport { moveFocus } from '../utils/focus';\nimport { useControlled } from '../utils/useControlled';\n\nexport interface ChoiceOption<V extends string = string> {\n value: V;\n label: string;\n icon?: ReactNode;\n disabled?: boolean;\n}\n\nexport interface ChoiceLabels {\n /** Trigger text while nothing is chosen. */\n placeholder: string;\n}\n\nexport const CHOICE_LABELS: ChoiceLabels = { placeholder: 'Scegli…' };\n\nexport interface ChoiceOwnProps<V extends string = string> {\n /** Visible label (\"Ordina per\"). */\n label: ReactNode;\n options: ChoiceOption<V>[];\n value?: V | null;\n defaultValue?: V | null;\n onValueChange?: (value: V) => void;\n /** Form field name: a hidden input carries the value. */\n name?: string;\n disabled?: boolean;\n /** Panel width. Default 280. */\n width?: number;\n placement?: BloomPlacement;\n labels?: Partial<ChoiceLabels>;\n}\n\nexport type ChoiceProps<\n V extends string = string,\n E extends ElementType = 'div',\n> = PolymorphicProps<E, ChoiceOwnProps<V>>;\n\n/**\n * Select: the trigger **becomes** the list (Bloom). ArrowDown / ArrowUp / Enter / Space open it on the trigger;\n * in the list arrows move, Enter or Space choose, Escape closes and gives focus back.\n */\nexport function Choice<V extends string = string, E extends ElementType = 'div'>(\n props: ChoiceProps<V, E>,\n) {\n const {\n as,\n css,\n className,\n label,\n options,\n value: valueProp,\n defaultValue = null,\n onValueChange,\n name,\n disabled,\n width = 280,\n placement,\n labels: labelsProp,\n ...rest\n } = useThemeProps('Choice', props as unknown as ChoiceProps);\n const labels = { ...CHOICE_LABELS, ...labelsProp };\n const [value, setValue] = useControlled<string | null>({\n value: valueProp,\n defaultValue,\n onChange: onValueChange as ((v: string | null) => void) | undefined,\n });\n const [open, setOpen] = useControlled({ value: undefined, defaultValue: false });\n const list = useRef<HTMLDivElement>(null);\n const labelId = useId();\n const Root = (as ?? 'div') as ElementType;\n const styled = useCss('Choice', 'root', css);\n const root = useAnimationRoot(rest);\n const chosen = options.find((o) => o.value === value);\n\n const choose = (o: ChoiceOption, close: (restore?: boolean) => void) => {\n if (o.disabled) return;\n setValue(o.value);\n close(true);\n };\n\n return (\n <>\n {styled.style}\n <Root className={cx('slurp-choice', styled.className, className)} {...root}>\n <span id={labelId} className=\"slurp-choice-label\">\n {label}\n </span>\n <Bloom\n open={open}\n onOpenChange={setOpen}\n role=\"listbox\"\n width={width}\n placement={placement}\n disabled={disabled}\n trigger={\n <>\n <span className={cx('slurp-choice-value', !chosen && 'slurp-choice-placeholder')}>\n {chosen?.label ?? labels.placeholder}\n </span>\n <ChevronDownIcon size={18} />\n </>\n }\n partProps={{\n trigger: {\n 'aria-labelledby': `${labelId} ${labelId}-value`,\n onKeyDown: (e: KeyboardEvent) => {\n if (!open && (e.key === 'ArrowDown' || e.key === 'ArrowUp')) {\n e.preventDefault();\n setOpen(true);\n }\n },\n },\n panel: { 'aria-labelledby': labelId },\n }}\n >\n {(close) => (\n <div\n ref={list}\n className=\"slurp-choice-list\"\n onKeyDown={(e) => {\n if (moveFocus(e, list.current, '[role=\"option\"]')) return;\n if (e.key === 'Enter' || e.key === ' ') {\n const at = (e.target as HTMLElement).closest<HTMLElement>('[role=\"option\"]');\n const o = options.find((x) => x.value === at?.dataset.value);\n if (o) {\n e.preventDefault();\n choose(o, close);\n }\n }\n }}\n >\n {options.map((o) => {\n const on = o.value === value;\n return (\n <div\n key={o.value}\n role=\"option\"\n tabIndex={-1}\n aria-selected={on}\n aria-disabled={o.disabled || undefined}\n data-value={o.value}\n className=\"slurp-bloom-item\"\n onClick={() => choose(o, close)}\n >\n {o.icon != null && (\n <span className=\"slurp-bloom-item-icon\" aria-hidden=\"true\">\n {o.icon}\n </span>\n )}\n <span className=\"slurp-bloom-item-label\">{o.label}</span>\n {on && (\n <span className=\"slurp-bloom-item-check\" aria-hidden=\"true\">\n <CheckIcon size={18} />\n </span>\n )}\n </div>\n );\n })}\n </div>\n )}\n </Bloom>\n <span id={`${labelId}-value`} hidden>\n {chosen?.label ?? labels.placeholder}\n </span>\n {name && <input type=\"hidden\" name={name} value={value ?? ''} />}\n </Root>\n </>\n );\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"Choice.js","names":[],"sources":["../../src/Choice/Choice.tsx"],"sourcesContent":["import { CheckIcon, ChevronDownIcon } from '@slurp-ui/icons';\nimport { cx } from '@slurp-ui/utils';\nimport { useId, useRef, type ElementType, type KeyboardEvent, type ReactNode } from 'react';\n\nimport { useLabels } from '../internal/labels';\nimport { Bloom, type BloomPlacement } from '../Bloom/Bloom';\nimport { useAnimationRoot } from '../internal/animation';\nimport { useCss, useThemeProps } from '../internal/styled';\nimport type { PolymorphicProps } from '../lib/polymorphic';\nimport { moveFocus } from '../utils/focus';\nimport { useControlled } from '../utils/useControlled';\n\nexport interface ChoiceOption<V extends string = string> {\n value: V;\n label: string;\n icon?: ReactNode;\n disabled?: boolean;\n}\n\nexport interface ChoiceLabels {\n /** Trigger text while nothing is chosen. */\n placeholder: string;\n}\n\nexport const CHOICE_LABELS: ChoiceLabels = { placeholder: 'Scegli…' };\n\nexport interface ChoiceOwnProps<V extends string = string> {\n /** Visible label (\"Ordina per\"). */\n label: ReactNode;\n options: ChoiceOption<V>[];\n value?: V | null;\n defaultValue?: V | null;\n onValueChange?: (value: V) => void;\n /** Form field name: a hidden input carries the value. */\n name?: string;\n disabled?: boolean;\n /** Panel width. Default 280. */\n width?: number;\n placement?: BloomPlacement;\n labels?: Partial<ChoiceLabels>;\n}\n\nexport type ChoiceProps<\n V extends string = string,\n E extends ElementType = 'div',\n> = PolymorphicProps<E, ChoiceOwnProps<V>>;\n\n/**\n * Select: the trigger **becomes** the list (Bloom). ArrowDown / ArrowUp / Enter / Space open it on the trigger;\n * in the list arrows move, Enter or Space choose, Escape closes and gives focus back.\n */\nexport function Choice<V extends string = string, E extends ElementType = 'div'>(\n props: ChoiceProps<V, E>,\n) {\n const {\n as,\n css,\n className,\n label,\n options,\n value: valueProp,\n defaultValue = null,\n onValueChange,\n name,\n disabled,\n width = 280,\n placement,\n labels: labelsProp,\n ...rest\n } = useThemeProps('Choice', props as unknown as ChoiceProps);\n const labels = { ...CHOICE_LABELS, ...useLabels('Choice'), ...labelsProp };\n const [value, setValue] = useControlled<string | null>({\n value: valueProp,\n defaultValue,\n onChange: onValueChange as ((v: string | null) => void) | undefined,\n });\n const [open, setOpen] = useControlled({ value: undefined, defaultValue: false });\n const list = useRef<HTMLDivElement>(null);\n const labelId = useId();\n const Root = (as ?? 'div') as ElementType;\n const styled = useCss('Choice', 'root', css);\n const root = useAnimationRoot(rest);\n const chosen = options.find((o) => o.value === value);\n\n const choose = (o: ChoiceOption, close: (restore?: boolean) => void) => {\n if (o.disabled) return;\n setValue(o.value);\n close(true);\n };\n\n return (\n <>\n {styled.style}\n <Root className={cx('slurp-choice', styled.className, className)} {...root}>\n <span id={labelId} className=\"slurp-choice-label\">\n {label}\n </span>\n <Bloom\n open={open}\n onOpenChange={setOpen}\n role=\"listbox\"\n width={width}\n placement={placement}\n disabled={disabled}\n trigger={\n <>\n <span className={cx('slurp-choice-value', !chosen && 'slurp-choice-placeholder')}>\n {chosen?.label ?? labels.placeholder}\n </span>\n <ChevronDownIcon size={18} />\n </>\n }\n partProps={{\n trigger: {\n 'aria-labelledby': `${labelId} ${labelId}-value`,\n onKeyDown: (e: KeyboardEvent) => {\n if (!open && (e.key === 'ArrowDown' || e.key === 'ArrowUp')) {\n e.preventDefault();\n setOpen(true);\n }\n },\n },\n panel: { 'aria-labelledby': labelId },\n }}\n >\n {(close) => (\n <div\n ref={list}\n className=\"slurp-choice-list\"\n onKeyDown={(e) => {\n if (moveFocus(e, list.current, '[role=\"option\"]')) return;\n if (e.key === 'Enter' || e.key === ' ') {\n const at = (e.target as HTMLElement).closest<HTMLElement>('[role=\"option\"]');\n const o = options.find((x) => x.value === at?.dataset.value);\n if (o) {\n e.preventDefault();\n choose(o, close);\n }\n }\n }}\n >\n {options.map((o) => {\n const on = o.value === value;\n return (\n <div\n key={o.value}\n role=\"option\"\n tabIndex={-1}\n aria-selected={on}\n aria-disabled={o.disabled || undefined}\n data-value={o.value}\n className=\"slurp-bloom-item\"\n onClick={() => choose(o, close)}\n >\n {o.icon != null && (\n <span className=\"slurp-bloom-item-icon\" aria-hidden=\"true\">\n {o.icon}\n </span>\n )}\n <span className=\"slurp-bloom-item-label\">{o.label}</span>\n {on && (\n <span className=\"slurp-bloom-item-check\" aria-hidden=\"true\">\n <CheckIcon size={18} />\n </span>\n )}\n </div>\n );\n })}\n </div>\n )}\n </Bloom>\n <span id={`${labelId}-value`} hidden>\n {chosen?.label ?? labels.placeholder}\n </span>\n {name && <input type=\"hidden\" name={name} value={value ?? ''} />}\n </Root>\n </>\n );\n}\n"],"mappings":"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"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
|
|
2
|
+
import { PartsProps, PolymorphicProps } from "../lib/polymorphic.js";
|
|
3
|
+
import { ElementType, ReactNode } from "react";
|
|
4
|
+
export interface CodeLabels {
|
|
5
|
+
/** Name of the copy button. */
|
|
6
|
+
copy: string;
|
|
7
|
+
/** Its name for two seconds after a copy. */
|
|
8
|
+
copied: string;
|
|
9
|
+
/** Name of the scrollable code block when there is no filename. */
|
|
10
|
+
region: string;
|
|
11
|
+
}
|
|
12
|
+
export declare const CODE_LABELS: CodeLabels;
|
|
13
|
+
export interface CodeOwnProps extends PartsProps<'bar' | 'filename' | 'copy' | 'pre'> {
|
|
14
|
+
/** The code as text: shown as is, and what Copy puts on the clipboard. */
|
|
15
|
+
code?: string;
|
|
16
|
+
/**
|
|
17
|
+
* The same code already highlighted (HTML from shiki, Prism…). Shown instead of `code`; `code` is still what gets
|
|
18
|
+
* copied (or the text of this markup when there is no `code`).
|
|
19
|
+
*/
|
|
20
|
+
html?: string;
|
|
21
|
+
/** Your own markup inside <code>, instead of `code` or `html`. */
|
|
22
|
+
children?: ReactNode;
|
|
23
|
+
/** A file name or a label above the code ("main.tsx", "Terminale"). */
|
|
24
|
+
filename?: ReactNode;
|
|
25
|
+
/** Language, for the record (`data-language`) and for highlighters. */
|
|
26
|
+
language?: string;
|
|
27
|
+
/** Show the copy button. Default true. */
|
|
28
|
+
copyable?: boolean;
|
|
29
|
+
labels?: Partial<CodeLabels>;
|
|
30
|
+
}
|
|
31
|
+
export type CodeProps<E extends ElementType = 'figure'> = PolymorphicProps<E, CodeOwnProps>;
|
|
32
|
+
/**
|
|
33
|
+
* A block of code: an optional file name, a copy button that says when it worked, and a region that scrolls on its
|
|
34
|
+
* own (reachable with Tab). Highlighting is yours to bring: pass `html` from any highlighter; its colours map onto
|
|
35
|
+
* the design system through `--slurp-code-*` (shiki's CSS variables are mapped too).
|
|
36
|
+
*/
|
|
37
|
+
export declare function Code<E extends ElementType = 'figure'>(props: CodeProps<E>): import("react").JSX.Element;
|
|
38
|
+
//# sourceMappingURL=Code.d.ts.map
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useLabels } from "../internal/labels.js";
|
|
3
|
+
import { cx } from "../utils/src/index.js";
|
|
4
|
+
import { useAnimationRoot } from "../internal/animation.js";
|
|
5
|
+
import { useCss, useParts, useThemeProps } from "../internal/styled.js";
|
|
6
|
+
import { Bubble } from "../Bubble/Bubble.js";
|
|
7
|
+
import { useClipboard } from "../utils/useClipboard.js";
|
|
8
|
+
import { useId } from "react";
|
|
9
|
+
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
|
|
10
|
+
import { CheckIcon, CopyIcon } from "@slurp-ui/icons";
|
|
11
|
+
// src/Code/Code.tsx
|
|
12
|
+
const CODE_LABELS = {
|
|
13
|
+
copy: "Copia il codice",
|
|
14
|
+
copied: "Copiato",
|
|
15
|
+
region: "Codice"
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* A block of code: an optional file name, a copy button that says when it worked, and a region that scrolls on its
|
|
19
|
+
* own (reachable with Tab). Highlighting is yours to bring: pass `html` from any highlighter; its colours map onto
|
|
20
|
+
* the design system through `--slurp-code-*` (shiki's CSS variables are mapped too).
|
|
21
|
+
*/
|
|
22
|
+
function Code(props) {
|
|
23
|
+
const { as, css, className, code, html, children, filename, language, copyable = true, labels: labelsProp, parts, partProps, classNames, ...rest } = useThemeProps("Code", props);
|
|
24
|
+
const labels = {
|
|
25
|
+
...CODE_LABELS,
|
|
26
|
+
...useLabels("Code"),
|
|
27
|
+
...labelsProp
|
|
28
|
+
};
|
|
29
|
+
const Root = as ?? "figure";
|
|
30
|
+
const styled = useCss("Code", "root", css);
|
|
31
|
+
const { part, style } = useParts("Code", {
|
|
32
|
+
parts,
|
|
33
|
+
partProps,
|
|
34
|
+
classNames
|
|
35
|
+
});
|
|
36
|
+
const root = useAnimationRoot(rest);
|
|
37
|
+
const { copy, copied } = useClipboard();
|
|
38
|
+
const bodyId = useId();
|
|
39
|
+
const regionName = typeof filename === "string" ? filename : language ?? labels.region;
|
|
40
|
+
const content = children ?? (html != null ? /* @__PURE__ */ jsx("code", { dangerouslySetInnerHTML: { __html: html } }) : /* @__PURE__ */ jsx("code", { children: code }));
|
|
41
|
+
return /* @__PURE__ */ jsxs(Fragment$1, { children: [
|
|
42
|
+
styled.style,
|
|
43
|
+
style,
|
|
44
|
+
/* @__PURE__ */ jsxs(Root, {
|
|
45
|
+
className: cx("slurp-code", styled.className, className),
|
|
46
|
+
"data-language": language,
|
|
47
|
+
...root,
|
|
48
|
+
children: [
|
|
49
|
+
(filename != null || copyable) && part("bar", "div", {
|
|
50
|
+
className: "slurp-code-bar",
|
|
51
|
+
children: /* @__PURE__ */ jsxs(Fragment$1, { children: [part("filename", "span", {
|
|
52
|
+
className: "slurp-code-filename",
|
|
53
|
+
children: filename
|
|
54
|
+
}), copyable && part("copy", Bubble, {
|
|
55
|
+
className: "slurp-code-copy",
|
|
56
|
+
size: "sm",
|
|
57
|
+
label: copied ? labels.copied : labels.copy,
|
|
58
|
+
"data-copied": copied || void 0,
|
|
59
|
+
onClick: () => void copy(code ?? document.getElementById(bodyId)?.textContent ?? ""),
|
|
60
|
+
children: copied ? /* @__PURE__ */ jsx(CheckIcon, { size: 18 }) : /* @__PURE__ */ jsx(CopyIcon, { size: 18 })
|
|
61
|
+
})] })
|
|
62
|
+
}),
|
|
63
|
+
part("pre", "pre", {
|
|
64
|
+
id: bodyId,
|
|
65
|
+
className: "slurp-code-pre",
|
|
66
|
+
tabIndex: 0,
|
|
67
|
+
role: "group",
|
|
68
|
+
"aria-label": regionName,
|
|
69
|
+
children: content
|
|
70
|
+
}),
|
|
71
|
+
copyable && /* @__PURE__ */ jsx("span", {
|
|
72
|
+
className: "slurp-visually-hidden",
|
|
73
|
+
role: "status",
|
|
74
|
+
children: copied ? labels.copied : ""
|
|
75
|
+
})
|
|
76
|
+
]
|
|
77
|
+
})
|
|
78
|
+
] });
|
|
79
|
+
}
|
|
80
|
+
export { CODE_LABELS, Code };
|
|
81
|
+
|
|
82
|
+
//# sourceMappingURL=Code.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Code.js","names":[],"sources":["../../src/Code/Code.tsx"],"sourcesContent":["import { CheckIcon, CopyIcon } from '@slurp-ui/icons';\nimport { cx } from '@slurp-ui/utils';\nimport { useId, type ElementType, type ReactNode } from 'react';\n\nimport { Bubble } from '../Bubble/Bubble';\nimport { useAnimationRoot } from '../internal/animation';\nimport { useLabels } from '../internal/labels';\nimport { useCss, useParts, useThemeProps } from '../internal/styled';\nimport type { PartsProps, PolymorphicProps } from '../lib/polymorphic';\nimport { useClipboard } from '../utils/useClipboard';\n\nexport interface CodeLabels {\n /** Name of the copy button. */\n copy: string;\n /** Its name for two seconds after a copy. */\n copied: string;\n /** Name of the scrollable code block when there is no filename. */\n region: string;\n}\n\nexport const CODE_LABELS: CodeLabels = {\n copy: 'Copia il codice',\n copied: 'Copiato',\n region: 'Codice',\n};\n\nexport interface CodeOwnProps extends PartsProps<'bar' | 'filename' | 'copy' | 'pre'> {\n /** The code as text: shown as is, and what Copy puts on the clipboard. */\n code?: string;\n /**\n * The same code already highlighted (HTML from shiki, Prism…). Shown instead of `code`; `code` is still what gets\n * copied (or the text of this markup when there is no `code`).\n */\n html?: string;\n /** Your own markup inside <code>, instead of `code` or `html`. */\n children?: ReactNode;\n /** A file name or a label above the code (\"main.tsx\", \"Terminale\"). */\n filename?: ReactNode;\n /** Language, for the record (`data-language`) and for highlighters. */\n language?: string;\n /** Show the copy button. Default true. */\n copyable?: boolean;\n labels?: Partial<CodeLabels>;\n}\n\nexport type CodeProps<E extends ElementType = 'figure'> = PolymorphicProps<E, CodeOwnProps>;\n\n/**\n * A block of code: an optional file name, a copy button that says when it worked, and a region that scrolls on its\n * own (reachable with Tab). Highlighting is yours to bring: pass `html` from any highlighter; its colours map onto\n * the design system through `--slurp-code-*` (shiki's CSS variables are mapped too).\n */\nexport function Code<E extends ElementType = 'figure'>(props: CodeProps<E>) {\n const {\n as,\n css,\n className,\n code,\n html,\n children,\n filename,\n language,\n copyable = true,\n labels: labelsProp,\n parts,\n partProps,\n classNames,\n ...rest\n } = useThemeProps('Code', props as CodeProps);\n const labels = { ...CODE_LABELS, ...useLabels('Code'), ...labelsProp };\n const Root = (as ?? 'figure') as ElementType;\n const styled = useCss('Code', 'root', css);\n const { part, style } = useParts('Code', { parts, partProps, classNames });\n const root = useAnimationRoot(rest);\n const { copy, copied } = useClipboard();\n const bodyId = useId();\n const regionName = typeof filename === 'string' ? filename : (language ?? labels.region);\n\n const content =\n children ??\n (html != null ? <code dangerouslySetInnerHTML={{ __html: html }} /> : <code>{code}</code>);\n\n return (\n <>\n {styled.style}\n {style}\n <Root\n className={cx('slurp-code', styled.className, className)}\n data-language={language}\n {...root}\n >\n {(filename != null || copyable) &&\n part('bar', 'div', {\n className: 'slurp-code-bar',\n children: (\n <>\n {part('filename', 'span', {\n className: 'slurp-code-filename',\n children: filename,\n })}\n {copyable &&\n part('copy', Bubble, {\n className: 'slurp-code-copy',\n size: 'sm',\n label: copied ? labels.copied : labels.copy,\n 'data-copied': copied || undefined,\n onClick: () =>\n void copy(code ?? document.getElementById(bodyId)?.textContent ?? ''),\n children: copied ? <CheckIcon size={18} /> : <CopyIcon size={18} />,\n })}\n </>\n ),\n })}\n {part('pre', 'pre', {\n id: bodyId,\n className: 'slurp-code-pre',\n tabIndex: 0,\n // A named group, not a region: a page has many code blocks, and landmarks must be unique.\n role: 'group',\n 'aria-label': regionName,\n children: content,\n })}\n {copyable && (\n <span className=\"slurp-visually-hidden\" role=\"status\">\n {copied ? labels.copied : ''}\n </span>\n )}\n </Root>\n </>\n );\n}\n"],"mappings":";;;;;;;;;;;AAoBA,MAAa,cAA0B;CACrC,MAAM;CACN,QAAQ;CACR,QAAQ;AACV;;;;;;AA4BA,SAAgB,KAAuC,OAAqB;CAC1E,MAAM,EACJ,IACA,KACA,WACA,MACA,MACA,UACA,UACA,UACA,WAAW,MACX,QAAQ,YACR,OACA,WACA,YACA,GAAG,SACD,cAAc,QAAQ,KAAkB;CAC5C,MAAM,SAAS;EAAE,GAAG;EAAa,GAAG,UAAU,MAAM;EAAG,GAAG;CAAW;CACrE,MAAM,OAAQ,MAAM;CACpB,MAAM,SAAS,OAAO,QAAQ,QAAQ,GAAG;CACzC,MAAM,EAAE,MAAM,UAAU,SAAS,QAAQ;EAAE;EAAO;EAAW;CAAW,CAAC;CACzE,MAAM,OAAO,iBAAiB,IAAI;CAClC,MAAM,EAAE,MAAM,WAAW,aAAa;CACtC,MAAM,SAAS,MAAM;CACrB,MAAM,aAAa,OAAO,aAAa,WAAW,WAAY,YAAY,OAAO;CAEjF,MAAM,UACJ,aACC,QAAQ,OAAO,oBAAC,QAAD,EAAM,yBAAyB,EAAE,QAAQ,KAAK,EAAI,CAAA,IAAI,oBAAC,QAAD,EAAA,UAAO,KAAW,CAAA;CAE1F,OACE,qBAAA,YAAA,EAAA,UAAA;EACG,OAAO;EACP;EACD,qBAAC,MAAD;GACE,WAAW,GAAG,cAAc,OAAO,WAAW,SAAS;GACvD,iBAAe;GACf,GAAI;GAHN,UAAA;KAKI,YAAY,QAAQ,aACpB,KAAK,OAAO,OAAO;KACjB,WAAW;KACX,UACE,qBAAA,YAAA,EAAA,UAAA,CACG,KAAK,YAAY,QAAQ;MACxB,WAAW;MACX,UAAU;KACZ,CAAC,GACA,YACC,KAAK,QAAQ,QAAQ;MACnB,WAAW;MACX,MAAM;MACN,OAAO,SAAS,OAAO,SAAS,OAAO;MACvC,eAAe,UAAU,KAAA;MACzB,eACE,KAAK,KAAK,QAAQ,SAAS,eAAe,MAAM,CAAC,EAAE,eAAe,EAAE;MACtE,UAAU,SAAS,oBAAC,WAAD,EAAW,MAAM,GAAK,CAAA,IAAI,oBAAC,UAAD,EAAU,MAAM,GAAK,CAAA;KACpE,CAAC,CACH,EAAA,CAAA;IAEN,CAAC;IACF,KAAK,OAAO,OAAO;KAClB,IAAI;KACJ,WAAW;KACX,UAAU;KAEV,MAAM;KACN,cAAc;KACd,UAAU;IACZ,CAAC;IACA,YACC,oBAAC,QAAD;KAAM,WAAU;KAAwB,MAAK;KAC1C,UAAA,SAAS,OAAO,SAAS;IACtB,CAAA;GAEJ;;CACN,EAAA,CAAA;AAEN"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
|
|
2
|
+
import { PartsProps, PolymorphicProps } from "../lib/polymorphic.js";
|
|
3
|
+
import { ElementType, ReactNode } from "react";
|
|
4
|
+
export interface ContentsLabels {
|
|
5
|
+
/** The visible title and the name of the navigation. */
|
|
6
|
+
title: string;
|
|
7
|
+
}
|
|
8
|
+
export declare const CONTENTS_LABELS: ContentsLabels;
|
|
9
|
+
export interface ContentsItem {
|
|
10
|
+
/** Id of the heading (the link goes to `#id`). */
|
|
11
|
+
id: string;
|
|
12
|
+
label: ReactNode;
|
|
13
|
+
/** 2 (default) or 3: a level-3 item is indented under its section. */
|
|
14
|
+
level?: 2 | 3;
|
|
15
|
+
}
|
|
16
|
+
export interface ContentsOwnProps extends PartsProps<'title' | 'list' | 'item' | 'link'> {
|
|
17
|
+
items: readonly ContentsItem[];
|
|
18
|
+
/**
|
|
19
|
+
* The section being read. Leave it out to follow the scroll (`useScrollSpy`); pass it to drive it yourself (`null`
|
|
20
|
+
* for none).
|
|
21
|
+
*/
|
|
22
|
+
active?: string | null;
|
|
23
|
+
/** Hide the visible title (the navigation keeps its name). */
|
|
24
|
+
hideTitle?: boolean;
|
|
25
|
+
/** Called when a link is followed (e.g. to close the panel it sits in). */
|
|
26
|
+
onNavigate?: (id: string) => void;
|
|
27
|
+
labels?: Partial<ContentsLabels>;
|
|
28
|
+
}
|
|
29
|
+
export type ContentsProps<E extends ElementType = 'nav'> = PolymorphicProps<E, ContentsOwnProps>;
|
|
30
|
+
/**
|
|
31
|
+
* "On this page": links to a page's sections, the one being read highlighted like a ListRow (accent-soft with an
|
|
32
|
+
* accent-ink outline) and marked `aria-current="location"`. Hidden with fewer than two items.
|
|
33
|
+
*/
|
|
34
|
+
export declare function Contents<E extends ElementType = 'nav'>(props: ContentsProps<E>): import("react").JSX.Element | null;
|
|
35
|
+
//# sourceMappingURL=Contents.d.ts.map
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useLabels } from "../internal/labels.js";
|
|
3
|
+
import { cx } from "../utils/src/index.js";
|
|
4
|
+
import { useAnimationRoot } from "../internal/animation.js";
|
|
5
|
+
import { useCss, useParts, useThemeProps } from "../internal/styled.js";
|
|
6
|
+
import { useScrollSpy } from "../utils/useScrollSpy.js";
|
|
7
|
+
import { Fragment } from "react";
|
|
8
|
+
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
|
|
9
|
+
// src/Contents/Contents.tsx
|
|
10
|
+
const CONTENTS_LABELS = { title: "In questa pagina" };
|
|
11
|
+
/**
|
|
12
|
+
* "On this page": links to a page's sections, the one being read highlighted like a ListRow (accent-soft with an
|
|
13
|
+
* accent-ink outline) and marked `aria-current="location"`. Hidden with fewer than two items.
|
|
14
|
+
*/
|
|
15
|
+
function Contents(props) {
|
|
16
|
+
const { as, css, className, items, active: activeProp, hideTitle = false, onNavigate, labels: labelsProp, parts, partProps, classNames, ...rest } = useThemeProps("Contents", props);
|
|
17
|
+
const labels = {
|
|
18
|
+
...CONTENTS_LABELS,
|
|
19
|
+
...useLabels("Contents"),
|
|
20
|
+
...labelsProp
|
|
21
|
+
};
|
|
22
|
+
const Root = as ?? "nav";
|
|
23
|
+
const styled = useCss("Contents", "root", css);
|
|
24
|
+
const { part, style } = useParts("Contents", {
|
|
25
|
+
parts,
|
|
26
|
+
partProps,
|
|
27
|
+
classNames
|
|
28
|
+
});
|
|
29
|
+
const root = useAnimationRoot(rest);
|
|
30
|
+
const spied = useScrollSpy(activeProp === void 0 ? items.map((i) => i.id) : []);
|
|
31
|
+
const active = activeProp === void 0 ? spied : activeProp;
|
|
32
|
+
if (items.length < 2) return null;
|
|
33
|
+
return /* @__PURE__ */ jsxs(Fragment$1, { children: [
|
|
34
|
+
styled.style,
|
|
35
|
+
style,
|
|
36
|
+
/* @__PURE__ */ jsxs(Root, {
|
|
37
|
+
className: cx("slurp-contents", styled.className, className),
|
|
38
|
+
"aria-label": labels.title,
|
|
39
|
+
...root,
|
|
40
|
+
children: [!hideTitle && part("title", "p", {
|
|
41
|
+
className: "slurp-contents-title",
|
|
42
|
+
"aria-hidden": true,
|
|
43
|
+
children: labels.title
|
|
44
|
+
}), part("list", "ul", {
|
|
45
|
+
className: "slurp-contents-list",
|
|
46
|
+
children: items.map((item) => /* @__PURE__ */ jsx(Fragment, { children: part("item", "li", {
|
|
47
|
+
className: "slurp-contents-item",
|
|
48
|
+
"data-level": item.level ?? 2,
|
|
49
|
+
children: part("link", "a", {
|
|
50
|
+
className: "slurp-contents-link",
|
|
51
|
+
href: `#${item.id}`,
|
|
52
|
+
"aria-current": item.id === active ? "location" : void 0,
|
|
53
|
+
onClick: () => onNavigate?.(item.id),
|
|
54
|
+
children: item.label
|
|
55
|
+
})
|
|
56
|
+
}) }, item.id))
|
|
57
|
+
})]
|
|
58
|
+
})
|
|
59
|
+
] });
|
|
60
|
+
}
|
|
61
|
+
export { CONTENTS_LABELS, Contents };
|
|
62
|
+
|
|
63
|
+
//# sourceMappingURL=Contents.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Contents.js","names":[],"sources":["../../src/Contents/Contents.tsx"],"sourcesContent":["import { cx } from '@slurp-ui/utils';\nimport { Fragment, type ElementType, type ReactNode } from 'react';\n\nimport { useAnimationRoot } from '../internal/animation';\nimport { useLabels } from '../internal/labels';\nimport { useCss, useParts, useThemeProps } from '../internal/styled';\nimport type { PartsProps, PolymorphicProps } from '../lib/polymorphic';\nimport { useScrollSpy } from '../utils/useScrollSpy';\n\nexport interface ContentsLabels {\n /** The visible title and the name of the navigation. */\n title: string;\n}\n\nexport const CONTENTS_LABELS: ContentsLabels = { title: 'In questa pagina' };\n\nexport interface ContentsItem {\n /** Id of the heading (the link goes to `#id`). */\n id: string;\n label: ReactNode;\n /** 2 (default) or 3: a level-3 item is indented under its section. */\n level?: 2 | 3;\n}\n\nexport interface ContentsOwnProps extends PartsProps<'title' | 'list' | 'item' | 'link'> {\n items: readonly ContentsItem[];\n /**\n * The section being read. Leave it out to follow the scroll (`useScrollSpy`); pass it to drive it yourself (`null`\n * for none).\n */\n active?: string | null;\n /** Hide the visible title (the navigation keeps its name). */\n hideTitle?: boolean;\n /** Called when a link is followed (e.g. to close the panel it sits in). */\n onNavigate?: (id: string) => void;\n labels?: Partial<ContentsLabels>;\n}\n\nexport type ContentsProps<E extends ElementType = 'nav'> = PolymorphicProps<E, ContentsOwnProps>;\n\n/**\n * \"On this page\": links to a page's sections, the one being read highlighted like a ListRow (accent-soft with an\n * accent-ink outline) and marked `aria-current=\"location\"`. Hidden with fewer than two items.\n */\nexport function Contents<E extends ElementType = 'nav'>(props: ContentsProps<E>) {\n const {\n as,\n css,\n className,\n items,\n active: activeProp,\n hideTitle = false,\n onNavigate,\n labels: labelsProp,\n parts,\n partProps,\n classNames,\n ...rest\n } = useThemeProps('Contents', props as ContentsProps);\n const labels = { ...CONTENTS_LABELS, ...useLabels('Contents'), ...labelsProp };\n const Root = (as ?? 'nav') as ElementType;\n const styled = useCss('Contents', 'root', css);\n const { part, style } = useParts('Contents', { parts, partProps, classNames });\n const root = useAnimationRoot(rest);\n const spied = useScrollSpy(activeProp === undefined ? items.map((i) => i.id) : []);\n const active = activeProp === undefined ? spied : activeProp;\n\n if (items.length < 2) return null;\n return (\n <>\n {styled.style}\n {style}\n <Root\n className={cx('slurp-contents', styled.className, className)}\n aria-label={labels.title}\n {...root}\n >\n {!hideTitle &&\n part('title', 'p', {\n className: 'slurp-contents-title',\n 'aria-hidden': true,\n children: labels.title,\n })}\n {part('list', 'ul', {\n className: 'slurp-contents-list',\n children: items.map((item) => (\n <Fragment key={item.id}>\n {part('item', 'li', {\n className: 'slurp-contents-item',\n 'data-level': item.level ?? 2,\n children: part('link', 'a', {\n className: 'slurp-contents-link',\n href: `#${item.id}`,\n 'aria-current': item.id === active ? 'location' : undefined,\n onClick: () => onNavigate?.(item.id),\n children: item.label,\n }),\n })}\n </Fragment>\n )),\n })}\n </Root>\n </>\n );\n}\n"],"mappings":";;;;;;;;;AAcA,MAAa,kBAAkC,EAAE,OAAO,mBAAmB;;;;;AA8B3E,SAAgB,SAAwC,OAAyB;CAC/E,MAAM,EACJ,IACA,KACA,WACA,OACA,QAAQ,YACR,YAAY,OACZ,YACA,QAAQ,YACR,OACA,WACA,YACA,GAAG,SACD,cAAc,YAAY,KAAsB;CACpD,MAAM,SAAS;EAAE,GAAG;EAAiB,GAAG,UAAU,UAAU;EAAG,GAAG;CAAW;CAC7E,MAAM,OAAQ,MAAM;CACpB,MAAM,SAAS,OAAO,YAAY,QAAQ,GAAG;CAC7C,MAAM,EAAE,MAAM,UAAU,SAAS,YAAY;EAAE;EAAO;EAAW;CAAW,CAAC;CAC7E,MAAM,OAAO,iBAAiB,IAAI;CAClC,MAAM,QAAQ,aAAa,eAAe,KAAA,IAAY,MAAM,KAAK,MAAM,EAAE,EAAE,IAAI,CAAC,CAAC;CACjF,MAAM,SAAS,eAAe,KAAA,IAAY,QAAQ;CAElD,IAAI,MAAM,SAAS,GAAG,OAAO;CAC7B,OACE,qBAAA,YAAA,EAAA,UAAA;EACG,OAAO;EACP;EACD,qBAAC,MAAD;GACE,WAAW,GAAG,kBAAkB,OAAO,WAAW,SAAS;GAC3D,cAAY,OAAO;GACnB,GAAI;GAHN,UAAA,CAKG,CAAC,aACA,KAAK,SAAS,KAAK;IACjB,WAAW;IACX,eAAe;IACf,UAAU,OAAO;GACnB,CAAC,GACF,KAAK,QAAQ,MAAM;IAClB,WAAW;IACX,UAAU,MAAM,KAAK,SACnB,oBAAC,UAAD,EAAA,UACG,KAAK,QAAQ,MAAM;KAClB,WAAW;KACX,cAAc,KAAK,SAAS;KAC5B,UAAU,KAAK,QAAQ,KAAK;MAC1B,WAAW;MACX,MAAM,IAAI,KAAK;MACf,gBAAgB,KAAK,OAAO,SAAS,aAAa,KAAA;MAClD,eAAe,aAAa,KAAK,EAAE;MACnC,UAAU,KAAK;KACjB,CAAC;IACH,CAAC,EACO,GAZK,KAAK,EAYV,CACX;GACH,CAAC,CACG;;CACN,EAAA,CAAA;AAEN"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
|
|
2
|
+
import { PolymorphicProps } from "../lib/polymorphic.js";
|
|
3
|
+
import { BloomPlacement } from "../Bloom/Bloom.js";
|
|
4
|
+
import { CalendarProps, CalendarValue } from "../Calendar/Calendar.js";
|
|
5
|
+
import { ElementType, ReactNode } from "react";
|
|
6
|
+
export interface DateFieldLabels {
|
|
7
|
+
/** Shown while nothing is chosen. */
|
|
8
|
+
placeholder: string;
|
|
9
|
+
}
|
|
10
|
+
export declare const DATEFIELD_LABELS: DateFieldLabels;
|
|
11
|
+
export interface DateFieldOwnProps extends Pick<CalendarProps, 'range' | 'min' | 'max' | 'isDateDisabled' | 'weekStartsOn'> {
|
|
12
|
+
label: ReactNode;
|
|
13
|
+
hint?: ReactNode;
|
|
14
|
+
value?: CalendarValue;
|
|
15
|
+
defaultValue?: CalendarValue;
|
|
16
|
+
onValueChange?: (value: CalendarValue) => void;
|
|
17
|
+
/** Form names: one hidden input with the day, or two (`<name>-start`, `<name>-end`) for a period. */
|
|
18
|
+
name?: string;
|
|
19
|
+
disabled?: boolean;
|
|
20
|
+
placement?: BloomPlacement;
|
|
21
|
+
labels?: Partial<DateFieldLabels>;
|
|
22
|
+
}
|
|
23
|
+
export type DateFieldProps<E extends ElementType = 'div'> = PolymorphicProps<E, DateFieldOwnProps>;
|
|
24
|
+
/**
|
|
25
|
+
* A date, or a period with `range`: the field shows it in the theme's language and grows into a Calendar (the Bloom
|
|
26
|
+
* rule: the control becomes the panel). It closes once a day, or a period's end, is picked.
|
|
27
|
+
*/
|
|
28
|
+
export declare function DateField<E extends ElementType = 'div'>(props: DateFieldProps<E>): import("react").JSX.Element;
|
|
29
|
+
//# sourceMappingURL=DateField.d.ts.map
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useTheme } from "../internal/context.js";
|
|
3
|
+
import { useLabels } from "../internal/labels.js";
|
|
4
|
+
import { cx } from "../utils/src/index.js";
|
|
5
|
+
import { useAnimationRoot } from "../internal/animation.js";
|
|
6
|
+
import { useCss, useThemeProps } from "../internal/styled.js";
|
|
7
|
+
import { useControlled } from "../utils/useControlled.js";
|
|
8
|
+
import { Bloom } from "../Bloom/Bloom.js";
|
|
9
|
+
import { toDate } from "../Calendar/dates.js";
|
|
10
|
+
import { Calendar } from "../Calendar/Calendar.js";
|
|
11
|
+
import { useId, useMemo } from "react";
|
|
12
|
+
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
|
|
13
|
+
import { CalendarIcon } from "@slurp-ui/icons";
|
|
14
|
+
// src/DateField/DateField.tsx
|
|
15
|
+
const DATEFIELD_LABELS = { placeholder: "Scegli una data" };
|
|
16
|
+
/**
|
|
17
|
+
* A date, or a period with `range`: the field shows it in the theme's language and grows into a Calendar (the Bloom
|
|
18
|
+
* rule: the control becomes the panel). It closes once a day, or a period's end, is picked.
|
|
19
|
+
*/
|
|
20
|
+
function DateField(props) {
|
|
21
|
+
const { as, css, className, label, hint, value: valueProp, defaultValue = null, onValueChange, range = false, min, max, isDateDisabled, weekStartsOn, name, disabled, placement, labels: labelsProp, ...rest } = useThemeProps("DateField", props);
|
|
22
|
+
const labels = {
|
|
23
|
+
...DATEFIELD_LABELS,
|
|
24
|
+
...useLabels("DateField"),
|
|
25
|
+
...labelsProp
|
|
26
|
+
};
|
|
27
|
+
const { locale } = useTheme();
|
|
28
|
+
const [value, setValue] = useControlled({
|
|
29
|
+
value: valueProp,
|
|
30
|
+
defaultValue,
|
|
31
|
+
onChange: onValueChange
|
|
32
|
+
});
|
|
33
|
+
const [open, setOpen] = useControlled({
|
|
34
|
+
value: void 0,
|
|
35
|
+
defaultValue: false
|
|
36
|
+
});
|
|
37
|
+
const id = useId();
|
|
38
|
+
const Root = as ?? "div";
|
|
39
|
+
const styled = useCss("DateField", "root", css);
|
|
40
|
+
const root = useAnimationRoot(rest);
|
|
41
|
+
const fmt = useMemo(() => new Intl.DateTimeFormat(locale, {
|
|
42
|
+
day: "numeric",
|
|
43
|
+
month: "long",
|
|
44
|
+
year: "numeric"
|
|
45
|
+
}), [locale]);
|
|
46
|
+
const text = value == null ? null : typeof value === "string" ? fmt.format(toDate(value)) : value.end ? fmt.formatRange(toDate(value.start), toDate(value.end)) : `${fmt.format(toDate(value.start))} –`;
|
|
47
|
+
return /* @__PURE__ */ jsxs(Fragment$1, { children: [styled.style, /* @__PURE__ */ jsxs(Root, {
|
|
48
|
+
className: cx("slurp-date-field", styled.className, className),
|
|
49
|
+
"data-disabled": disabled || void 0,
|
|
50
|
+
...root,
|
|
51
|
+
children: [
|
|
52
|
+
/* @__PURE__ */ jsx("span", {
|
|
53
|
+
className: "slurp-field-label",
|
|
54
|
+
id: `${id}-label`,
|
|
55
|
+
children: label
|
|
56
|
+
}),
|
|
57
|
+
/* @__PURE__ */ jsx(Bloom, {
|
|
58
|
+
open,
|
|
59
|
+
onOpenChange: setOpen,
|
|
60
|
+
role: "dialog",
|
|
61
|
+
width: 352,
|
|
62
|
+
placement,
|
|
63
|
+
disabled,
|
|
64
|
+
trigger: /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx(CalendarIcon, { size: 18 }), /* @__PURE__ */ jsx("span", {
|
|
65
|
+
className: cx("slurp-date-field-value", !text && "slurp-date-field-placeholder"),
|
|
66
|
+
children: text ?? labels.placeholder
|
|
67
|
+
})] }),
|
|
68
|
+
partProps: {
|
|
69
|
+
trigger: {
|
|
70
|
+
"aria-labelledby": `${id}-label ${id}-value`,
|
|
71
|
+
"aria-describedby": hint != null ? `${id}-hint` : void 0
|
|
72
|
+
},
|
|
73
|
+
panel: { "aria-labelledby": `${id}-label` }
|
|
74
|
+
},
|
|
75
|
+
children: (close) => /* @__PURE__ */ jsx(Calendar, {
|
|
76
|
+
className: "slurp-date-field-calendar",
|
|
77
|
+
value,
|
|
78
|
+
range,
|
|
79
|
+
min,
|
|
80
|
+
max,
|
|
81
|
+
isDateDisabled,
|
|
82
|
+
weekStartsOn,
|
|
83
|
+
autoFocus: true,
|
|
84
|
+
onValueChange: (next) => {
|
|
85
|
+
setValue(next);
|
|
86
|
+
if (next != null && (typeof next === "string" || next.end !== null)) close(true);
|
|
87
|
+
}
|
|
88
|
+
})
|
|
89
|
+
}),
|
|
90
|
+
/* @__PURE__ */ jsx("span", {
|
|
91
|
+
id: `${id}-value`,
|
|
92
|
+
hidden: true,
|
|
93
|
+
children: text ?? labels.placeholder
|
|
94
|
+
}),
|
|
95
|
+
hint != null && /* @__PURE__ */ jsx("p", {
|
|
96
|
+
className: "slurp-field-hint",
|
|
97
|
+
id: `${id}-hint`,
|
|
98
|
+
children: hint
|
|
99
|
+
}),
|
|
100
|
+
name != null && (range ? /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx("input", {
|
|
101
|
+
type: "hidden",
|
|
102
|
+
name: `${name}-start`,
|
|
103
|
+
value: value != null && typeof value === "object" ? value.start : ""
|
|
104
|
+
}), /* @__PURE__ */ jsx("input", {
|
|
105
|
+
type: "hidden",
|
|
106
|
+
name: `${name}-end`,
|
|
107
|
+
value: value != null && typeof value === "object" ? value.end ?? "" : ""
|
|
108
|
+
})] }) : /* @__PURE__ */ jsx("input", {
|
|
109
|
+
type: "hidden",
|
|
110
|
+
name,
|
|
111
|
+
value: typeof value === "string" ? value : ""
|
|
112
|
+
}))
|
|
113
|
+
]
|
|
114
|
+
})] });
|
|
115
|
+
}
|
|
116
|
+
export { DATEFIELD_LABELS, DateField };
|
|
117
|
+
|
|
118
|
+
//# sourceMappingURL=DateField.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DateField.js","names":[],"sources":["../../src/DateField/DateField.tsx"],"sourcesContent":["import { CalendarIcon } from '@slurp-ui/icons';\nimport { cx } from '@slurp-ui/utils';\nimport { useId, useMemo, type ElementType, type ReactNode } from 'react';\n\nimport { Bloom, type BloomPlacement } from '../Bloom/Bloom';\nimport { Calendar, type CalendarProps, type CalendarValue } from '../Calendar/Calendar';\nimport { toDate } from '../Calendar/dates';\nimport { useTheme } from '../internal/context';\nimport { useAnimationRoot } from '../internal/animation';\nimport { useLabels } from '../internal/labels';\nimport { useCss, useThemeProps } from '../internal/styled';\nimport type { PolymorphicProps } from '../lib/polymorphic';\nimport { useControlled } from '../utils/useControlled';\n\nexport interface DateFieldLabels {\n /** Shown while nothing is chosen. */\n placeholder: string;\n}\n\nexport const DATEFIELD_LABELS: DateFieldLabels = { placeholder: 'Scegli una data' };\n\nexport interface DateFieldOwnProps extends Pick<\n CalendarProps,\n 'range' | 'min' | 'max' | 'isDateDisabled' | 'weekStartsOn'\n> {\n label: ReactNode;\n hint?: ReactNode;\n value?: CalendarValue;\n defaultValue?: CalendarValue;\n onValueChange?: (value: CalendarValue) => void;\n /** Form names: one hidden input with the day, or two (`<name>-start`, `<name>-end`) for a period. */\n name?: string;\n disabled?: boolean;\n placement?: BloomPlacement;\n labels?: Partial<DateFieldLabels>;\n}\n\nexport type DateFieldProps<E extends ElementType = 'div'> = PolymorphicProps<E, DateFieldOwnProps>;\n\n/**\n * A date, or a period with `range`: the field shows it in the theme's language and grows into a Calendar (the Bloom\n * rule: the control becomes the panel). It closes once a day, or a period's end, is picked.\n */\nexport function DateField<E extends ElementType = 'div'>(props: DateFieldProps<E>) {\n const {\n as,\n css,\n className,\n label,\n hint,\n value: valueProp,\n defaultValue = null,\n onValueChange,\n range = false,\n min,\n max,\n isDateDisabled,\n weekStartsOn,\n name,\n disabled,\n placement,\n labels: labelsProp,\n ...rest\n } = useThemeProps('DateField', props as DateFieldProps);\n const labels = { ...DATEFIELD_LABELS, ...useLabels('DateField'), ...labelsProp };\n const { locale } = useTheme();\n const [value, setValue] = useControlled<CalendarValue>({\n value: valueProp,\n defaultValue,\n onChange: onValueChange,\n });\n const [open, setOpen] = useControlled({ value: undefined, defaultValue: false });\n const id = useId();\n const Root = (as ?? 'div') as ElementType;\n const styled = useCss('DateField', 'root', css);\n const root = useAnimationRoot(rest);\n const fmt = useMemo(\n () => new Intl.DateTimeFormat(locale, { day: 'numeric', month: 'long', year: 'numeric' }),\n [locale],\n );\n\n const text =\n value == null\n ? null\n : typeof value === 'string'\n ? fmt.format(toDate(value))\n : value.end\n ? fmt.formatRange(toDate(value.start), toDate(value.end))\n : `${fmt.format(toDate(value.start))} –`;\n\n return (\n <>\n {styled.style}\n <Root\n className={cx('slurp-date-field', styled.className, className)}\n data-disabled={disabled || undefined}\n {...root}\n >\n <span className=\"slurp-field-label\" id={`${id}-label`}>\n {label}\n </span>\n <Bloom\n open={open}\n onOpenChange={setOpen}\n role=\"dialog\"\n width={352}\n placement={placement}\n disabled={disabled}\n trigger={\n <>\n <CalendarIcon size={18} />\n <span\n className={cx('slurp-date-field-value', !text && 'slurp-date-field-placeholder')}\n >\n {text ?? labels.placeholder}\n </span>\n </>\n }\n partProps={{\n trigger: {\n 'aria-labelledby': `${id}-label ${id}-value`,\n 'aria-describedby': hint != null ? `${id}-hint` : undefined,\n },\n panel: { 'aria-labelledby': `${id}-label` },\n }}\n >\n {(close) => (\n <Calendar\n className=\"slurp-date-field-calendar\"\n value={value}\n range={range}\n min={min}\n max={max}\n isDateDisabled={isDateDisabled}\n weekStartsOn={weekStartsOn}\n autoFocus\n onValueChange={(next) => {\n setValue(next);\n if (next != null && (typeof next === 'string' || next.end !== null)) close(true);\n }}\n />\n )}\n </Bloom>\n <span id={`${id}-value`} hidden>\n {text ?? labels.placeholder}\n </span>\n {hint != null && (\n <p className=\"slurp-field-hint\" id={`${id}-hint`}>\n {hint}\n </p>\n )}\n {name != null &&\n (range ? (\n <>\n <input\n type=\"hidden\"\n name={`${name}-start`}\n value={value != null && typeof value === 'object' ? value.start : ''}\n />\n <input\n type=\"hidden\"\n name={`${name}-end`}\n value={value != null && typeof value === 'object' ? (value.end ?? '') : ''}\n />\n </>\n ) : (\n <input type=\"hidden\" name={name} value={typeof value === 'string' ? value : ''} />\n ))}\n </Root>\n </>\n );\n}\n"],"mappings":"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"}
|
package/dist/Dialog/Dialog.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
"use client";
|
|
2
|
+
import { useLabels } from "../internal/labels.js";
|
|
2
3
|
import { cx } from "../utils/src/index.js";
|
|
3
4
|
import { useForkRef } from "../utils/useForkRef.js";
|
|
4
5
|
import { useAnimationRoot } from "../internal/animation.js";
|
|
@@ -6,6 +7,7 @@ import { useCss, useParts, useThemeProps } from "../internal/styled.js";
|
|
|
6
7
|
import { Bubble } from "../Bubble/Bubble.js";
|
|
7
8
|
import { useControlled } from "../utils/useControlled.js";
|
|
8
9
|
import { useEffect, useId, useLayoutEffect, useRef } from "react";
|
|
10
|
+
import { duration, ease } from "@slurp-ui/tokens";
|
|
9
11
|
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
|
|
10
12
|
import { CloseIcon } from "@slurp-ui/icons";
|
|
11
13
|
// src/Dialog/Dialog.tsx
|
|
@@ -16,6 +18,7 @@ function useModal(props, variant) {
|
|
|
16
18
|
const { as, css, className, title, description, open: openProp, defaultOpen = false, onOpenChange, actions, dismissible = true, labels: labelsProp, children, parts, partProps, classNames, ref, ...rest } = props;
|
|
17
19
|
const labels = {
|
|
18
20
|
...DIALOG_LABELS,
|
|
21
|
+
...useLabels("Dialog"),
|
|
19
22
|
...labelsProp
|
|
20
23
|
};
|
|
21
24
|
const [open, setOpen] = useControlled({
|
|
@@ -37,19 +40,37 @@ function useModal(props, variant) {
|
|
|
37
40
|
...rest,
|
|
38
41
|
ref: useForkRef(ref, dialog)
|
|
39
42
|
});
|
|
43
|
+
const leaving = useRef(null);
|
|
40
44
|
useLayoutEffect(() => {
|
|
41
45
|
const el = dialog.current;
|
|
42
46
|
if (!el) return;
|
|
47
|
+
if (open && leaving.current) {
|
|
48
|
+
leaving.current.cancel();
|
|
49
|
+
leaving.current = null;
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
43
52
|
if (open && !el.open) {
|
|
44
53
|
opener.current = document.activeElement instanceof HTMLElement ? document.activeElement : null;
|
|
45
54
|
if (typeof el.showModal === "function") el.showModal();
|
|
46
55
|
else el.setAttribute("open", "");
|
|
47
56
|
grow(el, opener.current, variant);
|
|
48
|
-
} else if (!open && el.open) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
57
|
+
} else if (!open && el.open && !leaving.current) {
|
|
58
|
+
const close = () => {
|
|
59
|
+
leaving.current = null;
|
|
60
|
+
if (typeof el.close === "function") el.close();
|
|
61
|
+
else el.removeAttribute("open");
|
|
62
|
+
opener.current?.focus();
|
|
63
|
+
opener.current = null;
|
|
64
|
+
};
|
|
65
|
+
const exit = shrink(el, opener.current, variant);
|
|
66
|
+
if (!exit) return close();
|
|
67
|
+
leaving.current = exit;
|
|
68
|
+
exit.finished.then(() => {
|
|
69
|
+
if (leaving.current === exit) {
|
|
70
|
+
close();
|
|
71
|
+
exit.cancel();
|
|
72
|
+
}
|
|
73
|
+
}, () => {});
|
|
53
74
|
}
|
|
54
75
|
}, [open, variant]);
|
|
55
76
|
useEffect(() => () => opener.current?.focus(), []);
|
|
@@ -107,42 +128,69 @@ function useModal(props, variant) {
|
|
|
107
128
|
})
|
|
108
129
|
] });
|
|
109
130
|
}
|
|
131
|
+
/** Offset and uniform scale that put `el` on its opener's box (uniform, so the text never stretches). */
|
|
132
|
+
function fromOpener(el, from) {
|
|
133
|
+
const to = el.getBoundingClientRect();
|
|
134
|
+
const at = from?.isConnected ? from.getBoundingClientRect() : void 0;
|
|
135
|
+
if (!at || !to.width || !to.height || !at.width) return void 0;
|
|
136
|
+
return `translate(${at.left + at.width / 2 - (to.left + to.width / 2)}px, ${at.top + at.height / 2 - (to.top + to.height / 2)}px) scale(${Math.min(1, Math.max(.5, Math.min(at.width / to.width, at.height / to.height)))})`;
|
|
137
|
+
}
|
|
138
|
+
const EASE = ease.settle;
|
|
139
|
+
/** Milliseconds of a duration token (`'450ms'` → 450). */
|
|
140
|
+
const ms = (t) => Number.parseFloat(t);
|
|
110
141
|
/**
|
|
111
142
|
* The dialog grows out of the control that opened it (the Bloom rule applied to a modal): it starts at the opener's
|
|
112
|
-
*
|
|
143
|
+
* place and settles in the centre. A sheet rises from the bottom edge. Reduced motion: a 150ms fade, as the engine.
|
|
113
144
|
*/
|
|
114
145
|
function grow(el, from, variant) {
|
|
115
|
-
if (
|
|
146
|
+
if (typeof el.animate !== "function") return;
|
|
147
|
+
if (reduced()) {
|
|
148
|
+
el.animate([{ opacity: 0 }, { opacity: 1 }], { duration: 150 });
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
116
151
|
const timing = {
|
|
117
|
-
duration:
|
|
118
|
-
easing:
|
|
152
|
+
duration: ms(duration.panel),
|
|
153
|
+
easing: EASE
|
|
119
154
|
};
|
|
120
155
|
if (variant === "sheet") {
|
|
121
156
|
el.animate([{ transform: "translateY(100%)" }, { transform: "none" }], timing);
|
|
122
157
|
return;
|
|
123
158
|
}
|
|
124
|
-
const
|
|
125
|
-
const at = from?.getBoundingClientRect();
|
|
126
|
-
if (!at || !to.width || !to.height) {
|
|
127
|
-
el.animate([{
|
|
128
|
-
opacity: 0,
|
|
129
|
-
transform: "scale(0.96)"
|
|
130
|
-
}, {
|
|
131
|
-
opacity: 1,
|
|
132
|
-
transform: "none"
|
|
133
|
-
}], timing);
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
const dx = at.left + at.width / 2 - (to.left + to.width / 2);
|
|
137
|
-
const dy = at.top + at.height / 2 - (to.top + to.height / 2);
|
|
159
|
+
const start = fromOpener(el, from) ?? "scale(0.96)";
|
|
138
160
|
el.animate([{
|
|
139
|
-
transform:
|
|
140
|
-
opacity:
|
|
161
|
+
transform: start,
|
|
162
|
+
opacity: 0
|
|
141
163
|
}, {
|
|
142
164
|
transform: "none",
|
|
143
165
|
opacity: 1
|
|
144
166
|
}], timing);
|
|
145
167
|
}
|
|
168
|
+
/**
|
|
169
|
+
* The way out, quicker than the way in (200ms): back toward the opener, a sheet back down. Returns the animation to
|
|
170
|
+
* wait for before closing, or nothing when there is nothing to play.
|
|
171
|
+
*/
|
|
172
|
+
function shrink(el, to, variant) {
|
|
173
|
+
if (typeof el.animate !== "function") return void 0;
|
|
174
|
+
const fill = "forwards";
|
|
175
|
+
if (reduced()) return el.animate([{ opacity: 1 }, { opacity: 0 }], {
|
|
176
|
+
duration: 150,
|
|
177
|
+
fill
|
|
178
|
+
});
|
|
179
|
+
const timing = {
|
|
180
|
+
duration: ms(duration.quick),
|
|
181
|
+
easing: EASE,
|
|
182
|
+
fill
|
|
183
|
+
};
|
|
184
|
+
if (variant === "sheet") return el.animate([{ transform: "none" }, { transform: "translateY(100%)" }], timing);
|
|
185
|
+
const end = fromOpener(el, to) ?? "scale(0.96)";
|
|
186
|
+
return el.animate([{
|
|
187
|
+
transform: "none",
|
|
188
|
+
opacity: 1
|
|
189
|
+
}, {
|
|
190
|
+
transform: end,
|
|
191
|
+
opacity: 0
|
|
192
|
+
}], timing);
|
|
193
|
+
}
|
|
146
194
|
/** Modal dialog, centred, opened from a trigger: title, description, content and one primary action. */
|
|
147
195
|
function Dialog(props) {
|
|
148
196
|
return useModal(useThemeProps("Dialog", props), "dialog");
|