An open source design system that's fully customizable and agent ready
TypeScript
docs(useCollator): add a locale-aware name sort example block (#9510)
Parman Mohammadalizadeha34b886697f7
diff --git a/.changeset/use-collator-name-sort-example.md b/.changeset/use-collator-name-sort-example.mdnew file mode 100644index 0000000000..61a6b7beb5--- /dev/null+++ b/.changeset/use-collator-name-sort-example.md@@ -0,0 +1,7 @@+---+'@astryxdesign/cli': patch+---++[docs] A new `useCollator — Locale-aware Name Sort` example block sorts a list of names with the compare function from `useCollator`: accented and lowercase names land where a reader expects, and switching the provider locale to Swedish moves Å and Ö after Z. Copy it with `astryx template useCollatorHookUsage <path>`.++@MannXodiff --git a/apps/sandbox/scripts/next-export-routes.json b/apps/sandbox/scripts/next-export-routes.jsonindex 2ba661f602..465850db41 100644--- a/apps/sandbox/scripts/next-export-routes.json+++ b/apps/sandbox/scripts/next-export-routes.json@@ -780,6 +780,7 @@"/templates/table-tree/","/templates/table/","/templates/theme-showcase/",+ "/templates/useCollatorHookUsage/","/templates/useContainerRevealHookUsage/","/templates/useKeyboardHintHookUsage/","/templates/useStreamingTextHookUsage/",diff --git a/internal/shadcn-registry/routes.lock.json b/internal/shadcn-registry/routes.lock.jsonindex b09c556e16..975c464b79 100644--- a/internal/shadcn-registry/routes.lock.json+++ b/internal/shadcn-registry/routes.lock.json@@ -3644,6 +3644,11 @@"path": "examples/use-collapsible/custom-disclosure","aliases": []},+ {+ "name": "example-use-collator-locale-aware-name-sort",+ "path": "examples/use-collator/locale-aware-name-sort",+ "aliases": []+ },{"name": "example-use-container-reveal-reveal-on-hover-row-actions","path": "examples/use-container-reveal/reveal-on-hover-row-actions",diff --git a/packages/cli/api/template/use-collator-hook-usage.test.tsx b/packages/cli/api/template/use-collator-hook-usage.test.tsxnew file mode 100644index 0000000000..595885ec22--- /dev/null+++ b/packages/cli/api/template/use-collator-hook-usage.test.tsx@@ -0,0 +1,73 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.+// @vitest-environment jsdom++/**+ * The useCollator example sorts its names with the provider-bound collator,+ * so the order follows the locale: English keeps Å and Ö beside A and O,+ * Swedish moves them after Z.+ */++import {act} from 'react';+import {createRoot, type Root} from 'react-dom/client';+import {afterEach, describe, expect, it} from 'vitest';+import UseCollatorHookUsage from '../../assets/templates/blocks/components/Hooks/useCollatorHookUsage';++(globalThis as {IS_REACT_ACT_ENVIRONMENT?: boolean}).IS_REACT_ACT_ENVIRONMENT =+ true;++let root: Root | null = null;+let container: HTMLElement;++function renderExample(): void {+ container = document.createElement('div');+ document.body.appendChild(container);+ act(() => {+ root = createRoot(container);+ root.render(<UseCollatorHookUsage />);+ });+}++function names(): string[] {+ return Array.from(container.querySelectorAll('li'), item =>+ (item.textContent ?? '').trim(),+ );+}++afterEach(() => {+ act(() => root?.unmount());+ root = null;+ container.remove();+});++describe('useCollator example', () => {+ it('sorts accented and lowercase names for English', () => {+ renderExample();+ expect(names()).toEqual([+ 'Ana',+ 'Åsa',+ 'Émile',+ 'emma',+ 'Örjan',+ 'oscar',+ 'Zoë',+ ]);+ });++ it('re-sorts for Swedish when the provider locale changes', () => {+ renderExample();+ const swedish = Array.from(container.querySelectorAll('button')).find(+ button => button.textContent === 'Swedish',+ );+ expect(swedish).toBeDefined();+ act(() => swedish?.click());+ expect(names()).toEqual([+ 'Ana',+ 'Émile',+ 'emma',+ 'oscar',+ 'Zoë',+ 'Åsa',+ 'Örjan',+ ]);+ });+});diff --git a/packages/cli/assets/templates/blocks/components/Hooks/useCollatorHookUsage.doc.mjs b/packages/cli/assets/templates/blocks/components/Hooks/useCollatorHookUsage.doc.mjsnew file mode 100644index 0000000000..c5179566b6--- /dev/null+++ b/packages/cli/assets/templates/blocks/components/Hooks/useCollatorHookUsage.doc.mjs@@ -0,0 +1,20 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/** @type {import('@astryxdesign/cli/authoring').TemplateDoc} */+export const doc = {+ type: 'block',+ exampleFor: 'useCollator',+ name: 'useCollator — Locale-aware Name Sort',+ displayName: 'useCollator — Locale-aware Name Sort',+ description:+ 'A list of names sorted with the compare function from useCollator: accented and lowercase names land where a reader expects, and switching the provider locale to Swedish moves Å and Ö after Z.',+ isReady: true,+ aspectRatio: 4 / 3,+ componentsUsed: [+ 'InternationalizationProvider',+ 'Card',+ 'SegmentedControl',+ 'List',+ 'Layout',+ ],+};diff --git a/packages/cli/assets/templates/blocks/components/Hooks/useCollatorHookUsage.tsx b/packages/cli/assets/templates/blocks/components/Hooks/useCollatorHookUsage.tsxnew file mode 100644index 0000000000..bffdf0c7d8--- /dev/null+++ b/packages/cli/assets/templates/blocks/components/Hooks/useCollatorHookUsage.tsx@@ -0,0 +1,55 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++'use client';++import {useState} from 'react';+import {Card} from '@astryxdesign/core/Card';+import {+ InternationalizationProvider,+ useCollator,+} from '@astryxdesign/core/i18n';+import {VStack} from '@astryxdesign/core/Layout';+import {List, ListItem} from '@astryxdesign/core/List';+import {+ SegmentedControl,+ SegmentedControlItem,+} from '@astryxdesign/core/SegmentedControl';++type Locale = 'en' | 'sv-SE';++const PEOPLE = ['Zoë', 'emma', 'Örjan', 'Ana', 'Émile', 'oscar', 'Åsa'];++function PeopleList() {+ const collator = useCollator();+ const sorted = [...PEOPLE].sort(collator.compare);++ return (+ <List density="compact" hasDividers>+ {sorted.map(name => (+ <ListItem key={name} label={name} />+ ))}+ </List>+ );+}++export default function UseCollatorHookUsage() {+ const [locale, setLocale] = useState<Locale>('en');++ return (+ <InternationalizationProvider locale={locale}>+ <Card width={320} padding={4}>+ <VStack gap={3}>+ <SegmentedControl+ label="Sort order"+ value={locale}+ onChange={nextLocale => setLocale(nextLocale as Locale)}+ size="sm">+ <SegmentedControlItem value="en" label="English" />+ <SegmentedControlItem value="sv-SE" label="Swedish" />+ </SegmentedControl>+ <PeopleList />+ </VStack>+ </Card>+ </InternationalizationProvider>+ );+}