@sinnstack/ui 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +89 -0
- package/LICENSE +373 -0
- package/README.md +188 -2
- package/docs/blocks.md +373 -0
- package/docs/data.md +128 -0
- package/docs/finishes.md +165 -0
- package/docs/motion.md +166 -0
- package/docs/primitives.md +510 -0
- package/docs/seo.md +191 -0
- package/docs/tokens.md +122 -0
- package/package.json +132 -3
- package/scripts/check-contrast.mjs +180 -0
- package/src/blocks/BlockRenderer.astro +60 -0
- package/src/blocks/CTABanner.astro +121 -0
- package/src/blocks/CTAPair.astro +110 -0
- package/src/blocks/CardGrid.astro +167 -0
- package/src/blocks/ConsentEmbed.astro +125 -0
- package/src/blocks/ConsentManager.astro +147 -0
- package/src/blocks/ConsentScript.astro +47 -0
- package/src/blocks/ContactCard.astro +164 -0
- package/src/blocks/ContactForm.astro +224 -0
- package/src/blocks/ErrorPage.astro +242 -0
- package/src/blocks/FAQ.astro +135 -0
- package/src/blocks/FeatureGrid.astro +148 -0
- package/src/blocks/Hero.astro +171 -0
- package/src/blocks/HeroCarousel.astro +220 -0
- package/src/blocks/HighlightCards.astro +107 -0
- package/src/blocks/LocationHours.astro +311 -0
- package/src/blocks/LogoCloud.astro +135 -0
- package/src/blocks/MaintenancePage.astro +173 -0
- package/src/blocks/MediaText.astro +177 -0
- package/src/blocks/PageHeader.astro +126 -0
- package/src/blocks/ProseSection.astro +98 -0
- package/src/blocks/Reviews.astro +194 -0
- package/src/blocks/RichText.astro +58 -0
- package/src/blocks/StatsBand.astro +117 -0
- package/src/blocks/Steps.astro +141 -0
- package/src/blocks/Testimonials.astro +144 -0
- package/src/blocks/Topbar.astro +158 -0
- package/src/blocks/cms/card-grid.ts +48 -0
- package/src/blocks/cms/consent.ts +16 -0
- package/src/blocks/cms/contact-card.ts +46 -0
- package/src/blocks/cms/contact-form.ts +37 -0
- package/src/blocks/cms/cta-banner.ts +27 -0
- package/src/blocks/cms/cta-pair.ts +30 -0
- package/src/blocks/cms/data-base.ts +5 -0
- package/src/blocks/cms/data.ts +257 -0
- package/src/blocks/cms/faq.ts +38 -0
- package/src/blocks/cms/feature-grid.ts +47 -0
- package/src/blocks/cms/fixtures.ts +591 -0
- package/src/blocks/cms/hero-carousel.ts +36 -0
- package/src/blocks/cms/hero.ts +33 -0
- package/src/blocks/cms/highlight-cards.ts +41 -0
- package/src/blocks/cms/images.ts +37 -0
- package/src/blocks/cms/index.ts +59 -0
- package/src/blocks/cms/location-hours.ts +67 -0
- package/src/blocks/cms/logo-cloud.ts +36 -0
- package/src/blocks/cms/media-text.ts +26 -0
- package/src/blocks/cms/page-header.ts +27 -0
- package/src/blocks/cms/pages.ts +35 -0
- package/src/blocks/cms/prose-section.ts +25 -0
- package/src/blocks/cms/reviews.ts +50 -0
- package/src/blocks/cms/schema-dsl.ts +134 -0
- package/src/blocks/cms/schema.ts +794 -0
- package/src/blocks/cms/shared.ts +63 -0
- package/src/blocks/cms/stats-band.ts +35 -0
- package/src/blocks/cms/steps.ts +40 -0
- package/src/blocks/cms/testimonials.ts +46 -0
- package/src/blocks/cms/topbar.ts +27 -0
- package/src/blocks/cms/types.ts +27 -0
- package/src/blocks/consent.ts +14 -0
- package/src/blocks/icons.ts +47 -0
- package/src/blocks/index.ts +42 -0
- package/src/blocks/internal/Actions.astro +35 -0
- package/src/blocks/internal/BlockHeader.astro +45 -0
- package/src/blocks/internal/BlockShell.astro +58 -0
- package/src/blocks/internal/CarouselScript.astro +7 -0
- package/src/blocks/internal/ConsentEmbedScript.astro +7 -0
- package/src/blocks/internal/ConsentManagerScript.astro +7 -0
- package/src/blocks/internal/HoursScript.astro +7 -0
- package/src/blocks/internal/Placeholder.astro +13 -0
- package/src/blocks/internal/RichTextNode.astro +133 -0
- package/src/blocks/internal/StandaloneDocument.astro +53 -0
- package/src/blocks/internal/consent.ts +96 -0
- package/src/blocks/internal/fields.ts +18 -0
- package/src/blocks/internal/registry-contract.ts +67 -0
- package/src/blocks/internal/reviews.ts +22 -0
- package/src/blocks/internal/slug.ts +12 -0
- package/src/blocks/internal/util.ts +23 -0
- package/src/blocks/lib/contact.ts +92 -0
- package/src/blocks/lib/error-pages.ts +119 -0
- package/src/blocks/lib/faq.ts +37 -0
- package/src/blocks/lib/hours.ts +201 -0
- package/src/blocks/lib/maintenance.ts +129 -0
- package/src/blocks/lib/markdown.ts +350 -0
- package/src/blocks/lib/rich-text.ts +132 -0
- package/src/blocks/registry.ts +79 -0
- package/src/blocks/scripts/carousel.ts +218 -0
- package/src/blocks/scripts/consent-embed.ts +105 -0
- package/src/blocks/scripts/consent-manager.ts +112 -0
- package/src/blocks/scripts/consent-store.ts +86 -0
- package/src/blocks/scripts/error-page.ts +71 -0
- package/src/blocks/scripts/hours.ts +107 -0
- package/src/blocks/scripts/maintenance.ts +109 -0
- package/src/blocks/styles/blocks.css +1320 -0
- package/src/blocks/styles/consent.css +102 -0
- package/src/blocks/styles/faq.css +28 -0
- package/src/blocks/styles/location.css +92 -0
- package/src/blocks/styles/prose-section.css +13 -0
- package/src/blocks/styles/quotes.css +123 -0
- package/src/blocks/styles/rich-text.css +14 -0
- package/src/blocks/types.ts +293 -0
- package/src/data/ChartAxis.astro +19 -0
- package/src/data/ChartContainer.astro +152 -0
- package/src/data/ChartLegend.astro +25 -0
- package/src/data/ChartSeries.astro +14 -0
- package/src/data/ChartTable.astro +17 -0
- package/src/data/ChartTooltip.astro +20 -0
- package/src/data/Sparkline.astro +55 -0
- package/src/data/cms.ts +130 -0
- package/src/data/index.ts +10 -0
- package/src/data/internal/scale.ts +107 -0
- package/src/data/lib/chart.ts +459 -0
- package/src/data/lib/render.ts +219 -0
- package/src/data/lib/sparkline.ts +117 -0
- package/src/data/schema.ts +65 -0
- package/src/data/scripts/chart.ts +324 -0
- package/src/data/styles/chart.css +392 -0
- package/src/finishes/aurora.css +75 -0
- package/src/finishes/autumn.css +87 -0
- package/src/finishes/blueprint.css +69 -0
- package/src/finishes/brutalist.css +55 -0
- package/src/finishes/clay.css +48 -0
- package/src/finishes/day-cycle.css +46 -0
- package/src/finishes/fog.css +71 -0
- package/src/finishes/glass.css +149 -0
- package/src/finishes/holographic.css +91 -0
- package/src/finishes/night.css +77 -0
- package/src/finishes/paper.css +58 -0
- package/src/finishes/polished-glass.css +159 -0
- package/src/finishes/rain.css +84 -0
- package/src/finishes/scripts/day-cycle.ts +30 -0
- package/src/finishes/scripts/holographic.ts +37 -0
- package/src/finishes/scripts/spotlight.ts +30 -0
- package/src/finishes/spotlight.css +41 -0
- package/src/finishes/spring.css +80 -0
- package/src/finishes/terminal.css +49 -0
- package/src/motion/Marquee.astro +90 -0
- package/src/motion/MotionProvider.astro +50 -0
- package/src/motion/PageTransitions.astro +61 -0
- package/src/motion/Parallax.astro +44 -0
- package/src/motion/ParallaxLayer.astro +31 -0
- package/src/motion/RevealOnScroll.astro +59 -0
- package/src/motion/SceneRegistry.astro +38 -0
- package/src/motion/ScrollScene.astro +81 -0
- package/src/motion/index.ts +12 -0
- package/src/motion/integration.ts +48 -0
- package/src/motion/runtime/page-transitions.ts +68 -0
- package/src/motion/runtime/runtime.ts +180 -0
- package/src/motion/runtime/scenes.ts +248 -0
- package/src/motion/runtime/types.ts +103 -0
- package/src/motion/styles/motion.css +332 -0
- package/src/motion/transitions.ts +74 -0
- package/src/motion/virtual.d.ts +4 -0
- package/src/primitives/Accordion.astro +129 -0
- package/src/primitives/Breadcrumbs.astro +50 -0
- package/src/primitives/BulletList.astro +69 -0
- package/src/primitives/Button.astro +73 -0
- package/src/primitives/ButtonGroup.astro +54 -0
- package/src/primitives/Callout.astro +137 -0
- package/src/primitives/Caption.astro +35 -0
- package/src/primitives/Card.astro +72 -0
- package/src/primitives/CardContent.astro +24 -0
- package/src/primitives/CardFooter.astro +36 -0
- package/src/primitives/CardHeader.astro +86 -0
- package/src/primitives/Checkbox.astro +38 -0
- package/src/primitives/ChoiceGroup.astro +23 -0
- package/src/primitives/Cluster.astro +49 -0
- package/src/primitives/Container.astro +44 -0
- package/src/primitives/DescriptionList.astro +83 -0
- package/src/primitives/Dialog.astro +38 -0
- package/src/primitives/Disclosure.astro +67 -0
- package/src/primitives/Divider.astro +93 -0
- package/src/primitives/Drawer.astro +33 -0
- package/src/primitives/Fieldset.astro +80 -0
- package/src/primitives/Flex.astro +64 -0
- package/src/primitives/FocusRing.astro +63 -0
- package/src/primitives/FocusTrap.astro +64 -0
- package/src/primitives/Footer.astro +155 -0
- package/src/primitives/Form.astro +68 -0
- package/src/primitives/FormDescription.astro +26 -0
- package/src/primitives/FormError.astro +106 -0
- package/src/primitives/FormField.astro +72 -0
- package/src/primitives/Grid.astro +64 -0
- package/src/primitives/Heading.astro +45 -0
- package/src/primitives/IconButton.astro +48 -0
- package/src/primitives/Image.astro +125 -0
- package/src/primitives/Input.astro +86 -0
- package/src/primitives/Label.astro +50 -0
- package/src/primitives/Link.astro +81 -0
- package/src/primitives/LinkButton.astro +66 -0
- package/src/primitives/List.astro +39 -0
- package/src/primitives/ListItem.astro +178 -0
- package/src/primitives/MediaObject.astro +66 -0
- package/src/primitives/MetricCard.astro +278 -0
- package/src/primitives/NavLink.astro +81 -0
- package/src/primitives/NavMenu.astro +109 -0
- package/src/primitives/Navbar.astro +197 -0
- package/src/primitives/Panel.astro +153 -0
- package/src/primitives/Picture.astro +167 -0
- package/src/primitives/Popover.astro +95 -0
- package/src/primitives/Prose.astro +45 -0
- package/src/primitives/Radio.astro +21 -0
- package/src/primitives/Rating.astro +135 -0
- package/src/primitives/ReducedMotion.astro +90 -0
- package/src/primitives/SegmentedControl.astro +132 -0
- package/src/primitives/Select.astro +109 -0
- package/src/primitives/Sheet.astro +45 -0
- package/src/primitives/SkipLink.astro +31 -0
- package/src/primitives/Slider.astro +186 -0
- package/src/primitives/Spacer.astro +35 -0
- package/src/primitives/Stack.astro +51 -0
- package/src/primitives/Surface.astro +70 -0
- package/src/primitives/Switch.astro +22 -0
- package/src/primitives/Table.astro +382 -0
- package/src/primitives/Tabs.astro +126 -0
- package/src/primitives/Text.astro +56 -0
- package/src/primitives/Textarea.astro +75 -0
- package/src/primitives/Thumbnail.astro +154 -0
- package/src/primitives/Toggle.astro +99 -0
- package/src/primitives/Tooltip.astro +50 -0
- package/src/primitives/VisuallyHidden.astro +29 -0
- package/src/primitives/index.ts +103 -0
- package/src/primitives/internal/AccordionScript.astro +7 -0
- package/src/primitives/internal/ChoiceField.astro +91 -0
- package/src/primitives/internal/FocusTrapScript.astro +8 -0
- package/src/primitives/internal/FormValidationScript.astro +10 -0
- package/src/primitives/internal/IndeterminateScript.astro +13 -0
- package/src/primitives/internal/ModalPanel.astro +114 -0
- package/src/primitives/internal/OverlayScript.astro +7 -0
- package/src/primitives/internal/ReducedMotionScript.astro +7 -0
- package/src/primitives/internal/SliderScript.astro +7 -0
- package/src/primitives/internal/SparklineScript.astro +7 -0
- package/src/primitives/internal/TableSortScript.astro +7 -0
- package/src/primitives/internal/TabsScript.astro +7 -0
- package/src/primitives/internal/TooltipScript.astro +7 -0
- package/src/primitives/internal/image-plan.ts +82 -0
- package/src/primitives/internal/image.ts +129 -0
- package/src/primitives/internal/slider.ts +170 -0
- package/src/primitives/lib/deprecate.ts +47 -0
- package/src/primitives/lib/focus-trap.ts +197 -0
- package/src/primitives/lib/focus.ts +60 -0
- package/src/primitives/lib/motion.ts +68 -0
- package/src/primitives/scripts/accordion.ts +123 -0
- package/src/primitives/scripts/form-validation.ts +225 -0
- package/src/primitives/scripts/overlay.ts +71 -0
- package/src/primitives/scripts/reduced-motion.ts +59 -0
- package/src/primitives/scripts/slider.ts +150 -0
- package/src/primitives/scripts/sparkline.ts +131 -0
- package/src/primitives/scripts/table-sort.ts +68 -0
- package/src/primitives/scripts/tabs.ts +151 -0
- package/src/primitives/scripts/tooltip.ts +28 -0
- package/src/primitives/styles/a11y.css +104 -0
- package/src/primitives/styles/accordion.css +53 -0
- package/src/primitives/styles/breadcrumbs.css +59 -0
- package/src/primitives/styles/bullet-list.css +89 -0
- package/src/primitives/styles/button.css +237 -0
- package/src/primitives/styles/choice.css +164 -0
- package/src/primitives/styles/cluster.css +24 -0
- package/src/primitives/styles/container.css +19 -0
- package/src/primitives/styles/data.css +849 -0
- package/src/primitives/styles/disclosure.css +140 -0
- package/src/primitives/styles/divider.css +50 -0
- package/src/primitives/styles/flex.css +42 -0
- package/src/primitives/styles/form.css +241 -0
- package/src/primitives/styles/grid.css +36 -0
- package/src/primitives/styles/media.css +247 -0
- package/src/primitives/styles/nav.css +792 -0
- package/src/primitives/styles/overlay.css +362 -0
- package/src/primitives/styles/prose.css +146 -0
- package/src/primitives/styles/scale.css +175 -0
- package/src/primitives/styles/segmented.css +165 -0
- package/src/primitives/styles/slider.css +170 -0
- package/src/primitives/styles/spacer.css +17 -0
- package/src/primitives/styles/stack.css +25 -0
- package/src/primitives/styles/surface.css +403 -0
- package/src/primitives/styles/tabs.css +223 -0
- package/src/primitives/styles/typography.css +144 -0
- package/src/primitives/styles/utilities.css +24 -0
- package/src/primitives/types.ts +385 -0
- package/src/primitives/utils.ts +254 -0
- package/src/seo/JSONLD.astro +35 -0
- package/src/seo/SEOHead.astro +47 -0
- package/src/seo/cms.ts +59 -0
- package/src/seo/index.ts +10 -0
- package/src/seo/integration.ts +268 -0
- package/src/seo/internal/url.ts +68 -0
- package/src/seo/lib/jsonld.ts +342 -0
- package/src/seo/lib/meta.ts +205 -0
- package/src/seo/lib/redirects.ts +293 -0
- package/src/seo/lib/robots.ts +33 -0
- package/src/seo/lib/rss.ts +98 -0
- package/src/seo/lib/sitemap.ts +105 -0
- package/src/seo/schema.ts +43 -0
- package/src/tokens/base.css +71 -0
- package/src/tokens/tokens.css +210 -0
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* Table — information in rows and columns, rendered as a real `<table>` from `columns` + `rows`.
|
|
4
|
+
*
|
|
5
|
+
* Data representation
|
|
6
|
+
* - Values are formatted at render time with Intl (`format` per column: currency, percent,
|
|
7
|
+
* compact, date, delta…), so there's no client-side formatting code.
|
|
8
|
+
* - Numeric columns are end-aligned with tabular figures, so digits line up down a column.
|
|
9
|
+
* - `tone: 'delta'` colors a change by sign *and* adds an arrow (never color alone);
|
|
10
|
+
* `higherIsBetter: false` flips it for costs and wait times.
|
|
11
|
+
* - `bar: true` adds an inline data bar scaled to the column max (square baseline, rounded end).
|
|
12
|
+
* - `footer` renders a totals row (`<tfoot>`).
|
|
13
|
+
*
|
|
14
|
+
* Structure and access
|
|
15
|
+
* - `caption` is required (it names the table and its scroll region); `hideCaption` keeps it
|
|
16
|
+
* for screen readers only. The first `rowHeader` column becomes `<th scope="row">`.
|
|
17
|
+
* - Wide tables scroll inside a focusable, labelled region, with edge shadows that only
|
|
18
|
+
* appear while there's more to scroll (scroll-driven CSS, no JS). Sticky header always;
|
|
19
|
+
* `stickyFirst` pins the first column; `maxHeight` scrolls long tables in place.
|
|
20
|
+
* - `stack`: below ~36rem of width each row becomes a card of label/value pairs, with ARIA
|
|
21
|
+
* roles added so it's still announced as a table.
|
|
22
|
+
* - `hideBelow` on a column drops low-priority columns in narrow containers.
|
|
23
|
+
*
|
|
24
|
+
* Sorting
|
|
25
|
+
* - `sortable="client"`: header buttons sort rows in the browser (~1 KB script, loaded only
|
|
26
|
+
* on pages that use it). `aria-sort` and a polite announcement keep screen readers informed.
|
|
27
|
+
* - `sortable="link"`: headers are links (`?sort=key&dir=asc`); rows are sorted on the server.
|
|
28
|
+
* Works without JS; needs on-demand rendering (SSR) to react to the URL.
|
|
29
|
+
* - `sort` sets the initial order in either mode.
|
|
30
|
+
*
|
|
31
|
+
* States: `loading` (skeleton rows at final height, aria-busy) and an empty message.
|
|
32
|
+
* Rich cells: mark a column `slot: true` and pass a function in the `cell` slot.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* <Table caption="Claims this quarter" rows={claims} sortable="client" stack columns={[
|
|
36
|
+
* { key: 'provider', label: 'Provider', rowHeader: true },
|
|
37
|
+
* { key: 'date', label: 'Date', format: 'date' },
|
|
38
|
+
* { key: 'billed', label: 'Billed', format: 'currency' },
|
|
39
|
+
* { key: 'change', label: 'vs last qtr', format: 'delta-percent', tone: 'delta', higherIsBetter: false },
|
|
40
|
+
* ]} footer={{ provider: 'Total', billed: 12840 }} />
|
|
41
|
+
*/
|
|
42
|
+
import type { HTMLAttributes } from 'astro/types';
|
|
43
|
+
import type { FormatKind, TableColumn, ValueTone } from './types';
|
|
44
|
+
import { compareValues, deltaDirection, deltaIconSvg, deltaTone, escapeHtml, formatValue, pct, sortKey, statusIconSvg, uid, LEGACY_VALUE_TONES } from './utils';
|
|
45
|
+
import { renameValue } from './lib/deprecate';
|
|
46
|
+
import TableSortScript from './internal/TableSortScript.astro';
|
|
47
|
+
import './styles/scale.css';
|
|
48
|
+
import './styles/utilities.css';
|
|
49
|
+
import './styles/data.css';
|
|
50
|
+
|
|
51
|
+
type Row = Record<string, unknown>;
|
|
52
|
+
|
|
53
|
+
type Props = Omit<HTMLAttributes<'div'>, 'role'> & {
|
|
54
|
+
caption: string;
|
|
55
|
+
description?: string;
|
|
56
|
+
hideCaption?: boolean;
|
|
57
|
+
columns: TableColumn<Row>[];
|
|
58
|
+
rows: Row[];
|
|
59
|
+
/** Totals row, keyed like `rows`. */
|
|
60
|
+
footer?: Row;
|
|
61
|
+
/** Row key, for `currentKey`. */
|
|
62
|
+
rowKey?: string;
|
|
63
|
+
/** Highlight one row (e.g. "your plan"); sets aria-current="true". */
|
|
64
|
+
currentKey?: unknown;
|
|
65
|
+
sortable?: false | 'client' | 'link';
|
|
66
|
+
sort?: { key: string; direction: 'ascending' | 'descending' };
|
|
67
|
+
/** URL parameter names for link sorting. */
|
|
68
|
+
sortParams?: { key: string; direction: string };
|
|
69
|
+
variant?: 'outline' | 'plain';
|
|
70
|
+
density?: 'compact' | 'default' | 'comfortable';
|
|
71
|
+
zebra?: boolean;
|
|
72
|
+
hover?: boolean;
|
|
73
|
+
stack?: boolean;
|
|
74
|
+
stickyFirst?: boolean;
|
|
75
|
+
maxHeight?: 's' | 'm' | 'l';
|
|
76
|
+
loading?: boolean;
|
|
77
|
+
loadingRows?: number;
|
|
78
|
+
emptyMessage?: string;
|
|
79
|
+
locale?: string;
|
|
80
|
+
/** Screen-reader announcement after client-side sorting. `{column}` and `{direction}` are replaced. */
|
|
81
|
+
sortedTemplate?: string;
|
|
82
|
+
/** Translations of the sort directions used in the announcement. */
|
|
83
|
+
directionLabels?: { ascending: string; descending: string };
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const {
|
|
87
|
+
caption,
|
|
88
|
+
description,
|
|
89
|
+
hideCaption = false,
|
|
90
|
+
columns,
|
|
91
|
+
rows,
|
|
92
|
+
footer,
|
|
93
|
+
rowKey = 'id',
|
|
94
|
+
currentKey,
|
|
95
|
+
sortable = false,
|
|
96
|
+
sort: sortProp,
|
|
97
|
+
sortParams = { key: 'sort', direction: 'dir' },
|
|
98
|
+
variant = 'outline',
|
|
99
|
+
density = 'default',
|
|
100
|
+
zebra = false,
|
|
101
|
+
hover = true,
|
|
102
|
+
stack = false,
|
|
103
|
+
stickyFirst = false,
|
|
104
|
+
maxHeight,
|
|
105
|
+
loading = false,
|
|
106
|
+
loadingRows = 5,
|
|
107
|
+
emptyMessage = 'No results',
|
|
108
|
+
locale = 'en-US',
|
|
109
|
+
sortedTemplate = 'Sorted by {column}, {direction}',
|
|
110
|
+
directionLabels = { ascending: 'ascending', descending: 'descending' },
|
|
111
|
+
class: className,
|
|
112
|
+
...rest
|
|
113
|
+
} = Astro.props;
|
|
114
|
+
|
|
115
|
+
const id = uid('sinn-table');
|
|
116
|
+
const captionId = `${id}-caption`;
|
|
117
|
+
const descId = description ? `${id}-desc` : undefined;
|
|
118
|
+
const role = (r: 'table' | 'rowgroup' | 'row' | 'columnheader' | 'rowheader' | 'cell') => (stack ? r : undefined);
|
|
119
|
+
|
|
120
|
+
const NUMERIC_KINDS = new Set<FormatKind>(['number', 'integer', 'compact', 'currency', 'percent', 'delta', 'delta-percent']);
|
|
121
|
+
const kindOf = (c: TableColumn<Row>): FormatKind | undefined =>
|
|
122
|
+
typeof c.format === 'string' ? c.format : typeof c.format === 'object' ? c.format.kind : undefined;
|
|
123
|
+
const isNumeric = (c: TableColumn<Row>) => c.numeric ?? (kindOf(c) ? NUMERIC_KINDS.has(kindOf(c)!) : false);
|
|
124
|
+
const alignOf = (c: TableColumn<Row>) => c.align ?? (isNumeric(c) ? 'end' : 'start');
|
|
125
|
+
const isSortable = (c: TableColumn<Row>) => Boolean(sortable) && c.sortable !== false;
|
|
126
|
+
|
|
127
|
+
// ----- Current sort: prop, or the URL in link mode -----
|
|
128
|
+
let sort = sortProp;
|
|
129
|
+
if (sortable === 'link') {
|
|
130
|
+
const k = Astro.url.searchParams.get(sortParams.key);
|
|
131
|
+
const d = Astro.url.searchParams.get(sortParams.direction);
|
|
132
|
+
if (k && columns.some((c) => c.key === k)) {
|
|
133
|
+
sort = { key: k, direction: d === 'desc' || d === 'descending' ? 'descending' : 'ascending' };
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const valueFor = (c: TableColumn<Row>, row: Row) => (c.sortValue ? c.sortValue(row) : row[c.key]);
|
|
138
|
+
let sorted = rows;
|
|
139
|
+
if (sort) {
|
|
140
|
+
const col = columns.find((c) => c.key === sort!.key);
|
|
141
|
+
if (col) {
|
|
142
|
+
const dir = sort.direction === 'descending' ? -1 : 1;
|
|
143
|
+
sorted = rows
|
|
144
|
+
.map((row, i) => ({ row, i }))
|
|
145
|
+
.sort((a, b) => {
|
|
146
|
+
const va = valueFor(col, a.row);
|
|
147
|
+
const vb = valueFor(col, b.row);
|
|
148
|
+
const empty = (v: unknown) => v === null || v === undefined || v === '';
|
|
149
|
+
if (empty(va) !== empty(vb)) return empty(va) ? 1 : -1; // empties last either way
|
|
150
|
+
return compareValues(va, vb, locale) * dir || a.i - b.i;
|
|
151
|
+
})
|
|
152
|
+
.map((x) => x.row);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const sortHref = (key: string, next: 'ascending' | 'descending') => {
|
|
157
|
+
const url = new URL(Astro.url);
|
|
158
|
+
url.searchParams.set(sortParams.key, key);
|
|
159
|
+
url.searchParams.set(sortParams.direction, next === 'ascending' ? 'asc' : 'desc');
|
|
160
|
+
return `${url.pathname}${url.search}`;
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
// ----- Column maxima for data bars -----
|
|
164
|
+
const barMax = new Map<string, number>();
|
|
165
|
+
for (const c of columns) {
|
|
166
|
+
if (!c.bar) continue;
|
|
167
|
+
const max = c.barMax ?? Math.max(0, ...rows.map((r) => Math.abs(Number(r[c.key]) || 0)));
|
|
168
|
+
barMax.set(c.key, max);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// ----- Cell rendering (escaped HTML strings) -----
|
|
172
|
+
const hasCellSlot = Astro.slots.has('cell');
|
|
173
|
+
async function renderCell(c: TableColumn<Row>, row: Row, isFooter = false): Promise<string> {
|
|
174
|
+
const value = row[c.key];
|
|
175
|
+
const display =
|
|
176
|
+
typeof c.format === 'function' ? c.format(value, row) : formatValue(value, c.format ?? 'text', locale);
|
|
177
|
+
|
|
178
|
+
if (c.slot && hasCellSlot && !isFooter) {
|
|
179
|
+
const html = await Astro.slots.render('cell', [{ row, column: c, value, display }]);
|
|
180
|
+
if (html.trim()) return html;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
let tone: ValueTone | undefined;
|
|
184
|
+
let arrow = '';
|
|
185
|
+
let srPrefix = '';
|
|
186
|
+
let statusIcon = '';
|
|
187
|
+
let statusLabel = '';
|
|
188
|
+
if (c.tone === 'delta' && typeof value === 'number') {
|
|
189
|
+
tone = deltaTone(value, c.higherIsBetter ?? true);
|
|
190
|
+
const dir = deltaDirection(value);
|
|
191
|
+
arrow = deltaIconSvg(dir);
|
|
192
|
+
srPrefix = dir === 'up' ? 'Up ' : dir === 'down' ? 'Down ' : '';
|
|
193
|
+
} else if (typeof c.tone === 'function') {
|
|
194
|
+
tone = renameValue('Table', 'column tone', c.tone(value, row), LEGACY_VALUE_TONES);
|
|
195
|
+
if (tone === 'success' || tone === 'danger' || tone === 'warning') {
|
|
196
|
+
statusIcon = statusIconSvg(tone);
|
|
197
|
+
const defaults = { success: 'Good', danger: 'Flagged', warning: 'Warning' } as const;
|
|
198
|
+
statusLabel = c.toneLabel ? c.toneLabel(tone) : defaults[tone];
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
let text = escapeHtml(display);
|
|
203
|
+
if (arrow) {
|
|
204
|
+
const unsigned = escapeHtml(display.replace(/^[+−-]/, ''));
|
|
205
|
+
text = `<span class="sinn-delta">${arrow}<span aria-hidden="true">${text}</span><span class="sinn-sr-only">${srPrefix}${unsigned}</span></span>`;
|
|
206
|
+
}
|
|
207
|
+
if (statusIcon) {
|
|
208
|
+
text = `<span class="sinn-status-value">${statusIcon}<span>${text}</span><span class="sinn-sr-only"> (${escapeHtml(statusLabel)})</span></span>`;
|
|
209
|
+
}
|
|
210
|
+
const toned = tone && tone !== 'default' ? `<span data-value-tone="${tone}">${text}</span>` : text;
|
|
211
|
+
|
|
212
|
+
if (c.bar && !isFooter && typeof value === 'number') {
|
|
213
|
+
const p = pct(Math.abs(value), barMax.get(c.key) ?? 0);
|
|
214
|
+
return `<span class="sinn-table__barcell"><span class="sinn-bar" data-pct="${p}" aria-hidden="true"></span><span>${toned}</span></span>`;
|
|
215
|
+
}
|
|
216
|
+
return toned;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
interface CellOut { html: string; col: TableColumn<Row>; sortValue: string }
|
|
220
|
+
const bodyRows: Array<{ key: unknown; current: boolean; cells: CellOut[] }> = [];
|
|
221
|
+
for (const row of sorted) {
|
|
222
|
+
const cells: CellOut[] = [];
|
|
223
|
+
for (const c of columns) cells.push({ html: await renderCell(c, row), col: c, sortValue: sortKey(valueFor(c, row)) });
|
|
224
|
+
const key = row[rowKey];
|
|
225
|
+
bodyRows.push({ key, current: currentKey !== undefined && key === currentKey, cells });
|
|
226
|
+
}
|
|
227
|
+
const footCells: CellOut[] = [];
|
|
228
|
+
if (footer) for (const c of columns) footCells.push({ html: c.key in footer ? await renderCell(c, footer, true) : '', col: c, sortValue: '' });
|
|
229
|
+
|
|
230
|
+
const firstRowHeader = columns.findIndex((c) => c.rowHeader);
|
|
231
|
+
const sortIcon =
|
|
232
|
+
'<svg class="sinn-table__sort-icon" viewBox="0 0 10 14" aria-hidden="true" focusable="false"><path class="up" d="M5 1 9 6H1z"/><path class="down" d="M5 13 1 8h8z"/></svg>';
|
|
233
|
+
---
|
|
234
|
+
|
|
235
|
+
<div
|
|
236
|
+
class:list={['sinn-table', className]}
|
|
237
|
+
data-variant={variant === 'outline' ? undefined : variant}
|
|
238
|
+
data-density={density === 'default' ? undefined : density}
|
|
239
|
+
data-zebra={zebra ? '' : undefined}
|
|
240
|
+
data-hover={hover ? '' : undefined}
|
|
241
|
+
data-stack={stack ? '' : undefined}
|
|
242
|
+
data-sticky-first={stickyFirst ? '' : undefined}
|
|
243
|
+
data-max-height={maxHeight}
|
|
244
|
+
data-sinn-table={sortable === 'client' ? '' : undefined}
|
|
245
|
+
{...rest}
|
|
246
|
+
>
|
|
247
|
+
<div class="sinn-table__scroll" role="region" aria-labelledby={captionId} tabindex="0">
|
|
248
|
+
<table class="sinn-table__table" role={role('table')} aria-busy={loading ? 'true' : undefined} aria-describedby={descId} aria-labelledby={stack ? captionId : undefined}>
|
|
249
|
+
<caption id={captionId} class:list={['sinn-table__caption', { 'sinn-sr-only': hideCaption }]}>
|
|
250
|
+
<span class="sinn-table__title">{caption}</span>
|
|
251
|
+
{description && <span id={descId} class="sinn-table__description">{description}</span>}
|
|
252
|
+
</caption>
|
|
253
|
+
<thead role={role('rowgroup')}>
|
|
254
|
+
<tr role={role('row')}>
|
|
255
|
+
{
|
|
256
|
+
columns.map((c) => {
|
|
257
|
+
const active = sort?.key === c.key ? sort.direction : undefined;
|
|
258
|
+
const next = active === 'ascending' ? 'descending' : 'ascending';
|
|
259
|
+
return (
|
|
260
|
+
<th
|
|
261
|
+
scope="col"
|
|
262
|
+
role={role('columnheader')}
|
|
263
|
+
aria-sort={isSortable(c) ? active : undefined}
|
|
264
|
+
data-align={alignOf(c) === 'start' ? undefined : alignOf(c)}
|
|
265
|
+
data-numeric={isNumeric(c) ? '' : undefined}
|
|
266
|
+
data-width={c.width && c.width !== 'auto' ? c.width : undefined}
|
|
267
|
+
data-hide-below={c.hideBelow}
|
|
268
|
+
data-sort-type={isNumeric(c) || kindOf(c) === 'date' || kindOf(c) === 'datetime' ? 'number' : 'text'}
|
|
269
|
+
>
|
|
270
|
+
{isSortable(c) && sortable === 'client' ? (
|
|
271
|
+
<button type="button" class="sinn-table__sort">
|
|
272
|
+
<span>{c.label}</span>
|
|
273
|
+
<Fragment set:html={sortIcon} />
|
|
274
|
+
</button>
|
|
275
|
+
) : isSortable(c) && sortable === 'link' ? (
|
|
276
|
+
<a class="sinn-table__sort" href={sortHref(c.key, next)}>
|
|
277
|
+
<span>{c.label}</span>
|
|
278
|
+
<Fragment set:html={sortIcon} />
|
|
279
|
+
</a>
|
|
280
|
+
) : c.hideLabel ? (
|
|
281
|
+
<span class="sinn-sr-only">{c.label}</span>
|
|
282
|
+
) : (
|
|
283
|
+
c.label
|
|
284
|
+
)}
|
|
285
|
+
</th>
|
|
286
|
+
);
|
|
287
|
+
})
|
|
288
|
+
}
|
|
289
|
+
</tr>
|
|
290
|
+
</thead>
|
|
291
|
+
<tbody role={role('rowgroup')}>
|
|
292
|
+
{
|
|
293
|
+
loading ? (
|
|
294
|
+
Array.from({ length: loadingRows }, () => (
|
|
295
|
+
<tr role={role('row')} aria-hidden="true">
|
|
296
|
+
{columns.map((c) => (
|
|
297
|
+
<td role={role('cell')} data-align={alignOf(c) === 'start' ? undefined : alignOf(c)} data-hide-below={c.hideBelow}>
|
|
298
|
+
<span class="sinn-skeleton" />
|
|
299
|
+
</td>
|
|
300
|
+
))}
|
|
301
|
+
</tr>
|
|
302
|
+
))
|
|
303
|
+
) : bodyRows.length === 0 ? (
|
|
304
|
+
<tr role={role('row')}>
|
|
305
|
+
<td role={role('cell')} colspan={columns.length} class="sinn-table__empty">
|
|
306
|
+
<slot name="empty">{emptyMessage}</slot>
|
|
307
|
+
</td>
|
|
308
|
+
</tr>
|
|
309
|
+
) : (
|
|
310
|
+
bodyRows.map((r) => (
|
|
311
|
+
<tr role={role('row')} aria-current={r.current ? 'true' : undefined}>
|
|
312
|
+
{r.cells.map((cell, i) =>
|
|
313
|
+
i === firstRowHeader ? (
|
|
314
|
+
<th
|
|
315
|
+
scope="row"
|
|
316
|
+
role={role('rowheader')}
|
|
317
|
+
data-value={cell.sortValue}
|
|
318
|
+
data-align={alignOf(cell.col) === 'start' ? undefined : alignOf(cell.col)}
|
|
319
|
+
data-numeric={isNumeric(cell.col) ? '' : undefined}
|
|
320
|
+
data-hide-below={cell.col.hideBelow}
|
|
321
|
+
>
|
|
322
|
+
<Fragment set:html={cell.html} />
|
|
323
|
+
</th>
|
|
324
|
+
) : (
|
|
325
|
+
<td
|
|
326
|
+
role={role('cell')}
|
|
327
|
+
data-label={cell.col.label}
|
|
328
|
+
data-value={cell.sortValue}
|
|
329
|
+
data-align={alignOf(cell.col) === 'start' ? undefined : alignOf(cell.col)}
|
|
330
|
+
data-numeric={isNumeric(cell.col) ? '' : undefined}
|
|
331
|
+
data-hide-below={cell.col.hideBelow}
|
|
332
|
+
>
|
|
333
|
+
<Fragment set:html={cell.html} />
|
|
334
|
+
</td>
|
|
335
|
+
),
|
|
336
|
+
)}
|
|
337
|
+
</tr>
|
|
338
|
+
))
|
|
339
|
+
)
|
|
340
|
+
}
|
|
341
|
+
</tbody>
|
|
342
|
+
{
|
|
343
|
+
footer && !loading && (
|
|
344
|
+
<tfoot role={role('rowgroup')}>
|
|
345
|
+
<tr role={role('row')}>
|
|
346
|
+
{footCells.map((cell, i) =>
|
|
347
|
+
i === firstRowHeader ? (
|
|
348
|
+
<th scope="row" role={role('rowheader')} data-align={alignOf(cell.col) === 'start' ? undefined : alignOf(cell.col)} data-hide-below={cell.col.hideBelow}>
|
|
349
|
+
<Fragment set:html={cell.html} />
|
|
350
|
+
</th>
|
|
351
|
+
) : (
|
|
352
|
+
<td
|
|
353
|
+
role={role('cell')}
|
|
354
|
+
data-label={cell.html ? cell.col.label : undefined}
|
|
355
|
+
data-align={alignOf(cell.col) === 'start' ? undefined : alignOf(cell.col)}
|
|
356
|
+
data-numeric={isNumeric(cell.col) ? '' : undefined}
|
|
357
|
+
data-hide-below={cell.col.hideBelow}
|
|
358
|
+
>
|
|
359
|
+
<Fragment set:html={cell.html} />
|
|
360
|
+
</td>
|
|
361
|
+
),
|
|
362
|
+
)}
|
|
363
|
+
</tr>
|
|
364
|
+
</tfoot>
|
|
365
|
+
)
|
|
366
|
+
}
|
|
367
|
+
</table>
|
|
368
|
+
</div>
|
|
369
|
+
{
|
|
370
|
+
sortable === 'client' && (
|
|
371
|
+
<p
|
|
372
|
+
class="sinn-sr-only"
|
|
373
|
+
aria-live="polite"
|
|
374
|
+
data-sinn-table-status
|
|
375
|
+
data-template={sortedTemplate}
|
|
376
|
+
data-ascending={directionLabels.ascending}
|
|
377
|
+
data-descending={directionLabels.descending}
|
|
378
|
+
/>
|
|
379
|
+
)
|
|
380
|
+
}
|
|
381
|
+
</div>
|
|
382
|
+
{sortable === 'client' && <TableSortScript />}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* Tabs — switches between related panels in one place: a service's Overview / Pricing /
|
|
4
|
+
* FAQ, a provider's Bio / Locations / Reviews.
|
|
5
|
+
*
|
|
6
|
+
* Rendered as in-page links and panels, so it already works with no JavaScript: links
|
|
7
|
+
* switch panels through `:target`, and every panel's content is in the HTML for search
|
|
8
|
+
* engines (find-in-page only searches the visible panel). A ~1.4 KB script turns it into
|
|
9
|
+
* ARIA tabs (WAI-ARIA APG): one tab stop, ←/→ (↑/↓ when vertical), Home/End, automatic or manual activation.
|
|
10
|
+
* The same panel is visible before and after the script runs, so nothing shifts.
|
|
11
|
+
*
|
|
12
|
+
* `/page#pricing` opens with that tab selected, and any link to `#pricing` on the page
|
|
13
|
+
* switches to it. `syncHash` also writes the selected tab into the URL.
|
|
14
|
+
*
|
|
15
|
+
* Content: the slot named after each item's `id`, or the item's `content` (plain text).
|
|
16
|
+
* Use SegmentedControl for a form choice, Accordion when several sections may be wanted
|
|
17
|
+
* at once or the content is long.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* <Tabs label="Web services" items={[
|
|
21
|
+
* { id: 'overview', label: 'Overview' },
|
|
22
|
+
* { id: 'pricing', label: 'Pricing' },
|
|
23
|
+
* { id: 'faq', label: 'FAQ', badge: 12 },
|
|
24
|
+
* ]}>
|
|
25
|
+
* <Fragment slot="overview">…</Fragment>
|
|
26
|
+
* <Fragment slot="pricing">…</Fragment>
|
|
27
|
+
* <Fragment slot="faq">…</Fragment>
|
|
28
|
+
* </Tabs>
|
|
29
|
+
*/
|
|
30
|
+
import type { HTMLAttributes } from 'astro/types';
|
|
31
|
+
import type { TabItem } from './types';
|
|
32
|
+
import { uid } from './utils';
|
|
33
|
+
import TabsScript from './internal/TabsScript.astro';
|
|
34
|
+
import './styles/tabs.css';
|
|
35
|
+
import { renameProp, renameValue } from './lib/deprecate';
|
|
36
|
+
|
|
37
|
+
type Props = HTMLAttributes<'div'> & {
|
|
38
|
+
items: TabItem[];
|
|
39
|
+
/** Accessible name for the tab list ("Account settings"). */
|
|
40
|
+
label: string;
|
|
41
|
+
/** The `id` of the initially selected tab. Default: the first enabled one. */
|
|
42
|
+
selected?: string;
|
|
43
|
+
/** `automatic` (default): arrow keys select. `manual`: arrows move focus, Enter/Space selects. Use manual when showing a panel is slow. */
|
|
44
|
+
activation?: 'automatic' | 'manual';
|
|
45
|
+
/** `vertical` puts the tabs beside the panels when there's room (36rem+), above them when not. */
|
|
46
|
+
orientation?: 'horizontal' | 'vertical';
|
|
47
|
+
/** `underline` or `pill`. `pills` is the deprecated spelling (NavMenu says `pill`). */
|
|
48
|
+
variant?: 'underline' | 'pill' | 'pills';
|
|
49
|
+
size?: 's' | 'm' | 'l';
|
|
50
|
+
/** Stretch the tabs to fill the row (horizontal), the same name Button, ButtonGroup and SegmentedControl use. */
|
|
51
|
+
fullWidth?: boolean;
|
|
52
|
+
/** @deprecated Use `fullWidth`. */
|
|
53
|
+
fitted?: boolean;
|
|
54
|
+
/** Write the selected tab's id into the URL (replaceState: no extra history entries). */
|
|
55
|
+
syncHash?: boolean;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const {
|
|
59
|
+
items,
|
|
60
|
+
label,
|
|
61
|
+
selected,
|
|
62
|
+
activation = 'automatic',
|
|
63
|
+
orientation = 'horizontal',
|
|
64
|
+
variant: variantProp = 'underline',
|
|
65
|
+
size = 'm',
|
|
66
|
+
fullWidth: fullWidthProp,
|
|
67
|
+
fitted,
|
|
68
|
+
syncHash = false,
|
|
69
|
+
id = uid('sinn-tabs'),
|
|
70
|
+
class: className,
|
|
71
|
+
...rest
|
|
72
|
+
} = Astro.props;
|
|
73
|
+
|
|
74
|
+
const variant = renameValue('Tabs', 'variant', variantProp, { pills: 'pill' });
|
|
75
|
+
const fullWidth = renameProp('Tabs', 'fullWidth', fullWidthProp, 'fitted', fitted) ?? false;
|
|
76
|
+
|
|
77
|
+
if (import.meta.env?.DEV) {
|
|
78
|
+
if (!items.length) console.warn('[sinn] Tabs has no items; nothing is rendered.');
|
|
79
|
+
if (items.length > 12) console.warn(`[sinn] Tabs has ${items.length} tabs. The no-JavaScript fallback supports 12; past ~7, consider an Accordion or separate pages.`);
|
|
80
|
+
const ids = items.map((t) => t.id);
|
|
81
|
+
if (new Set(ids).size !== ids.length) console.warn('[sinn] Tabs: item ids must be unique.');
|
|
82
|
+
}
|
|
83
|
+
let current = items.findIndex((t) => t.id === selected && !t.disabled);
|
|
84
|
+
if (current < 0) current = Math.max(0, items.findIndex((t) => !t.disabled));
|
|
85
|
+
const panels = await Promise.all(
|
|
86
|
+
items.map(async (t) => (Astro.slots.has(t.id) ? await Astro.slots.render(t.id) : null)),
|
|
87
|
+
);
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
{
|
|
91
|
+
items.length > 0 && (
|
|
92
|
+
<div
|
|
93
|
+
id={id}
|
|
94
|
+
class:list={['sinn-tabs', className]}
|
|
95
|
+
data-orientation={orientation}
|
|
96
|
+
data-variant={variant}
|
|
97
|
+
data-size={size}
|
|
98
|
+
data-activation={activation}
|
|
99
|
+
data-sync-hash={syncHash ? '' : undefined}
|
|
100
|
+
data-label={label}
|
|
101
|
+
data-sinn-tabs
|
|
102
|
+
{...rest}
|
|
103
|
+
>
|
|
104
|
+
<div class="sinn-tabs__list" data-full-width={fullWidth ? '' : undefined}>
|
|
105
|
+
{items.map((t, i) => (
|
|
106
|
+
<a
|
|
107
|
+
class="sinn-tabs__tab"
|
|
108
|
+
id={`${t.id}-tab`}
|
|
109
|
+
href={t.disabled ? undefined : `#${t.id}`}
|
|
110
|
+
data-selected={i === current ? '' : undefined}
|
|
111
|
+
aria-disabled={t.disabled ? 'true' : undefined}
|
|
112
|
+
>
|
|
113
|
+
<span class="sinn-tabs__label">{t.label}</span>
|
|
114
|
+
{t.badge !== undefined && t.badge !== '' && <span class="sinn-tabs__badge">{t.badge}</span>}
|
|
115
|
+
</a>
|
|
116
|
+
))}
|
|
117
|
+
</div>
|
|
118
|
+
{items.map((t, i) => (
|
|
119
|
+
<div class="sinn-tabs__panel" id={t.id} data-selected={i === current ? '' : undefined} data-print-label={t.label} tabindex="-1">
|
|
120
|
+
{panels[i] ? <Fragment set:html={panels[i]} /> : t.content && <p>{t.content}</p>}
|
|
121
|
+
</div>
|
|
122
|
+
))}
|
|
123
|
+
</div>
|
|
124
|
+
)
|
|
125
|
+
}
|
|
126
|
+
<TabsScript />
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* Text — body text on the type scale.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* <Text>Paragraph.</Text>
|
|
7
|
+
* <Text as="span" size="s" tone="muted" weight="medium">Inline note</Text>
|
|
8
|
+
* <Text lines={3} measure>Long excerpt clamped to three lines…</Text>
|
|
9
|
+
*/
|
|
10
|
+
import type { HTMLAttributes, HTMLTag } from 'astro/types';
|
|
11
|
+
import type { FontWeight, TextAlign, TextSize, TextTone } from './types';
|
|
12
|
+
import './styles/utilities.css';
|
|
13
|
+
import './styles/typography.css';
|
|
14
|
+
|
|
15
|
+
type Props = HTMLAttributes<'p'> & {
|
|
16
|
+
/** Element to render. `p` for paragraphs, `span` inline, `div` when it wraps block content. */
|
|
17
|
+
as?: HTMLTag;
|
|
18
|
+
size?: TextSize;
|
|
19
|
+
tone?: TextTone;
|
|
20
|
+
weight?: FontWeight;
|
|
21
|
+
align?: TextAlign;
|
|
22
|
+
/** Cut to one line with an ellipsis. */
|
|
23
|
+
truncate?: boolean;
|
|
24
|
+
/** Clamp to this many lines. */
|
|
25
|
+
lines?: 2 | 3 | 4 | 5;
|
|
26
|
+
/** Cap line length at a readable measure (~65ch). */
|
|
27
|
+
measure?: boolean;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const {
|
|
31
|
+
as: Tag = 'p',
|
|
32
|
+
size = 'm',
|
|
33
|
+
tone = 'default',
|
|
34
|
+
weight,
|
|
35
|
+
align,
|
|
36
|
+
truncate = false,
|
|
37
|
+
lines,
|
|
38
|
+
measure = false,
|
|
39
|
+
class: className,
|
|
40
|
+
...rest
|
|
41
|
+
} = Astro.props;
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
<Tag
|
|
45
|
+
class:list={['sinn-type', 'sinn-text', className]}
|
|
46
|
+
data-size={size}
|
|
47
|
+
data-tone={tone === 'default' ? undefined : tone}
|
|
48
|
+
data-weight={weight}
|
|
49
|
+
data-align={align}
|
|
50
|
+
data-truncate={truncate ? '' : undefined}
|
|
51
|
+
data-lines={truncate ? undefined : lines}
|
|
52
|
+
data-measure={measure ? '' : undefined}
|
|
53
|
+
{...rest}
|
|
54
|
+
>
|
|
55
|
+
<slot />
|
|
56
|
+
</Tag>
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* Textarea — multi-line text entry.
|
|
4
|
+
*
|
|
5
|
+
* - `autosize` grows the box with its content via `field-sizing: content`
|
|
6
|
+
* (Chromium 123+; other browsers keep the `rows` height and stay resizable).
|
|
7
|
+
* - Set the initial text with `value` (not children).
|
|
8
|
+
* - Use `maxlength` for hard limits; a live character counter needs JS and isn't included.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* <Textarea name="message" label="How can we help?" rows={5} autosize required />
|
|
12
|
+
*/
|
|
13
|
+
import type { HTMLAttributes } from 'astro/types';
|
|
14
|
+
import type { FieldContext, FieldProps } from './types';
|
|
15
|
+
import { uid } from './utils';
|
|
16
|
+
import FormField from './FormField.astro';
|
|
17
|
+
import './styles/utilities.css';
|
|
18
|
+
import './styles/form.css';
|
|
19
|
+
|
|
20
|
+
type Props = HTMLAttributes<'textarea'> &
|
|
21
|
+
FieldProps & {
|
|
22
|
+
resize?: 'vertical' | 'none' | 'both';
|
|
23
|
+
autosize?: boolean;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const {
|
|
27
|
+
label,
|
|
28
|
+
description,
|
|
29
|
+
error,
|
|
30
|
+
hideLabel,
|
|
31
|
+
optional,
|
|
32
|
+
messages,
|
|
33
|
+
rows = 4,
|
|
34
|
+
resize = 'vertical',
|
|
35
|
+
autosize = false,
|
|
36
|
+
id: idProp,
|
|
37
|
+
required = false,
|
|
38
|
+
value,
|
|
39
|
+
'aria-describedby': describedBy,
|
|
40
|
+
class: className,
|
|
41
|
+
...rest
|
|
42
|
+
} = Astro.props;
|
|
43
|
+
// `??`, not a default: a default only replaces undefined, and id={null} would give "null-error" ids.
|
|
44
|
+
const id = idProp ?? uid('sinn-textarea');
|
|
45
|
+
|
|
46
|
+
const wrapped = Boolean(label || description || error);
|
|
47
|
+
// `class` goes on the field wrapper, or on the control itself when there is no wrapper.
|
|
48
|
+
const controlClass = wrapped ? undefined : className;
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
<FormField
|
|
52
|
+
id={id}
|
|
53
|
+
label={label}
|
|
54
|
+
description={description}
|
|
55
|
+
error={error}
|
|
56
|
+
hideLabel={hideLabel}
|
|
57
|
+
optional={optional}
|
|
58
|
+
required={Boolean(required)}
|
|
59
|
+
messages={messages}
|
|
60
|
+
aria-describedby={describedBy as string | undefined}
|
|
61
|
+
class={className}
|
|
62
|
+
>
|
|
63
|
+
{
|
|
64
|
+
(field: FieldContext) => (
|
|
65
|
+
<textarea
|
|
66
|
+
class:list={['sinn-control', controlClass]}
|
|
67
|
+
rows={rows}
|
|
68
|
+
data-resize={resize === 'vertical' ? undefined : resize}
|
|
69
|
+
data-autosize={autosize ? '' : undefined}
|
|
70
|
+
{...field.attrs}
|
|
71
|
+
{...rest}
|
|
72
|
+
>{value ?? ''}</textarea>
|
|
73
|
+
)
|
|
74
|
+
}
|
|
75
|
+
</FormField>
|