diff --git a/.changeset/docs-name-exported-types.md b/.changeset/docs-name-exported-types.md new file mode 100644 index 0000000000000..56faf5d0c01b3 --- /dev/null +++ b/.changeset/docs-name-exported-types.md @@ -0,0 +1,7 @@ +--- +'@astryxdesign/core': patch +--- + +[docs] Four docs name prop types the package exports. Selector's `options` are `SelectorOptionType[]` (`SelectorOption` is a component), SyntaxTheme's `theme` is a `SyntaxThemeDefinition`, and `useImperativeDialog` and `useImperativeAlertDialog` write their `show()` options with the exported Dialog and AlertDialog props. + +@cixzhang diff --git a/apps/docsite/src/__tests__/component-prop-controls.test.ts b/apps/docsite/src/__tests__/component-prop-controls.test.ts index c93865aa61fc5..1f95e09234246 100644 --- a/apps/docsite/src/__tests__/component-prop-controls.test.ts +++ b/apps/docsite/src/__tests__/component-prop-controls.test.ts @@ -90,6 +90,12 @@ describe('component detail prop controls', () => { } }); + it('gives a SyntaxThemeDefinition prop the syntax theme preset control', () => { + expect(parsePropType('SyntaxThemeDefinition', 'theme')).toEqual({ + kind: 'syntax-theme', + }); + }); + it('derives IconType options from the canonical icon registry', () => { const control = parsePropType('IconType', 'labelIcon'); diff --git a/apps/docsite/src/__tests__/syntax-theme-control.test.ts b/apps/docsite/src/__tests__/syntax-theme-control.test.ts new file mode 100644 index 0000000000000..d07475fe0b898 --- /dev/null +++ b/apps/docsite/src/__tests__/syntax-theme-control.test.ts @@ -0,0 +1,42 @@ +// Copyright (c) Meta Platforms, Inc. and affiliates. + +import {allSyntaxPresets} from '@astryxdesign/core/theme/syntax'; +import {describe, expect, it} from 'vitest'; +import { + NO_SYNTAX_THEME, + selectedSyntaxTheme, + syntaxThemeChoices, + syntaxPresetName, + syntaxThemeFor, +} from '../components/component-detail/syntaxThemeChoices'; + +describe('the playground syntax theme control', () => { + it('shows an unset optional prop as None, and None sends nothing', () => { + expect(syntaxThemeChoices(false)[0].value).toBe(NO_SYNTAX_THEME); + expect(selectedSyntaxTheme(undefined, false)).toBe(NO_SYNTAX_THEME); + expect(syntaxThemeFor(NO_SYNTAX_THEME)).toBeUndefined(); + }); + + it('offers no None for a required prop, which shows the first preset', () => { + expect( + syntaxThemeChoices(true).some(choice => choice.value === NO_SYNTAX_THEME), + ).toBe(false); + expect(selectedSyntaxTheme(undefined, true)).toBe(allSyntaxPresets[0].name); + }); + + it('shows and sends a chosen preset', () => { + const preset = allSyntaxPresets[1]; + expect(selectedSyntaxTheme(preset, false)).toBe(preset.name); + expect(syntaxThemeFor(preset.name)).toBe(preset); + }); + + it('names the preset a value is', () => { + const dracula = allSyntaxPresets.find(theme => theme.name === 'dracula'); + const oneDarkPro = allSyntaxPresets.find( + theme => theme.name === 'one-dark-pro', + ); + expect(syntaxPresetName(dracula)).toBe('dracula'); + expect(syntaxPresetName(oneDarkPro)).toBe('one-dark-pro'); + expect(syntaxPresetName({name: 'dracula'})).toBeNull(); + }); +}); diff --git a/apps/docsite/src/components/component-detail/InteractivePreview.tsx b/apps/docsite/src/components/component-detail/InteractivePreview.tsx index 1d18ef87b23fb..0a1c1c2c2a011 100644 --- a/apps/docsite/src/components/component-detail/InteractivePreview.tsx +++ b/apps/docsite/src/components/component-detail/InteractivePreview.tsx @@ -14,6 +14,7 @@ import { } from 'react'; import * as stylex from '@stylexjs/stylex'; import {getComponent, resolveValue} from './resolveElements'; +import {syntaxPresetName} from './syntaxThemeChoices'; import {AppShellMobileContext} from '@astryxdesign/core/AppShell'; import {Button} from '@astryxdesign/core/Button'; import {Card} from '@astryxdesign/core/Card'; @@ -122,6 +123,10 @@ function formatValue( if (value === null) { return 'null'; } + const syntaxPreset = syntaxPresetName(value); + if (syntaxPreset != null) { + return toIdentifierName(syntaxPreset); + } if ( value != null && typeof value === 'object' && diff --git a/apps/docsite/src/components/component-detail/PlaygroundPropsTable.tsx b/apps/docsite/src/components/component-detail/PlaygroundPropsTable.tsx index 07a8d3736e6ba..5af36961f033a 100644 --- a/apps/docsite/src/components/component-detail/PlaygroundPropsTable.tsx +++ b/apps/docsite/src/components/component-detail/PlaygroundPropsTable.tsx @@ -22,9 +22,13 @@ import {Badge} from '@astryxdesign/core/Badge'; import {IconButton} from '@astryxdesign/core/IconButton'; import {Minus, Plus} from 'lucide-react'; import {useMediaQuery} from '@astryxdesign/core/hooks'; -import {allSyntaxPresets} from '@astryxdesign/core/theme/syntax'; import {themeObjectsFull} from '../../generated/themeRegistry'; import {coerceEnumOption, type PropControlDescriptor} from './parsePropType'; +import { + selectedSyntaxTheme, + syntaxThemeChoices, + syntaxThemeFor, +} from './syntaxThemeChoices'; import {resolveTypeRefs, TypeRefText} from './TypeRefText'; import type {KnobProp} from './InteractivePreview'; import {resolveElementDescriptor} from './resolveElements'; @@ -166,15 +170,6 @@ const themeOptions = Object.entries(themeObjectsFull).map( }), ); -const syntaxThemeOptions = allSyntaxPresets.map(theme => ({ - value: theme.name, - label: theme.name - .split('-') - .map(part => part.charAt(0).toUpperCase() + part.slice(1)) - .join(' '), - theme, -})); - function ThemeControl({ value, onChange, @@ -217,36 +212,20 @@ function ThemeControl({ function SyntaxThemeControl({ value, + isRequired, onChange, }: { value: unknown; + isRequired: boolean; onChange: (next: unknown) => void; }) { - const selected = - syntaxThemeOptions.find( - option => - option.theme === value || - (value != null && - typeof value === 'object' && - 'name' in value && - option.theme.name === value.name), - )?.value ?? syntaxThemeOptions[0].value; - return ( ({ - value: optionValue, - label, - }))} - onChange={next => { - const match = syntaxThemeOptions.find(option => option.value === next); - if (match) { - onChange(match.theme); - } - }} + value={selectedSyntaxTheme(value, isRequired)} + options={syntaxThemeChoices(isRequired)} + onChange={next => onChange(syntaxThemeFor(next))} /> ); } @@ -475,7 +454,13 @@ function InlineControl({ case 'theme': return ; case 'syntax-theme': - return ; + return ( + + ); case 'string': return ( ({ + value: theme.name, + label: theme.name + .split('-') + .map(part => part.charAt(0).toUpperCase() + part.slice(1)) + .join(' '), + theme, +})); + +/** + * The presets, with a first choice that leaves the prop unset when the prop + * is optional. An unset prop keeps the theme-level syntax colors. + */ +export function syntaxThemeChoices( + isRequired: boolean, +): {value: string; label: string}[] { + const choices = presets.map(({value, label}) => ({value, label})); + return isRequired + ? choices + : [{value: NO_SYNTAX_THEME, label: NO_SYNTAX_THEME}, ...choices]; +} + +/** The choice that shows `value`. */ +export function selectedSyntaxTheme( + value: unknown, + isRequired: boolean, +): string { + const match = presets.find( + option => + option.theme === value || + (value != null && + typeof value === 'object' && + 'name' in value && + option.theme.name === value.name), + ); + if (match) { + return match.value; + } + return isRequired ? presets[0].value : NO_SYNTAX_THEME; +} + +/** + * The name of the preset `value` is, such as "one-dark-pro"; null when + * `value` is not a preset. + */ +export function syntaxPresetName(value: unknown): string | null { + return presets.find(option => option.theme === value)?.value ?? null; +} + +/** The value a choice sends: a preset, or nothing for the unset choice. */ +export function syntaxThemeFor(choice: string): unknown { + return presets.find(option => option.value === choice)?.theme; +} diff --git a/docs/specs/components/Toast/Toast.spec.md b/docs/specs/components/Toast/Toast.spec.md index bc15176405341..ca4c477caab0e 100644 --- a/docs/specs/components/Toast/Toast.spec.md +++ b/docs/specs/components/Toast/Toast.spec.md @@ -70,7 +70,8 @@ This table is a review projection; the body below is authoritative. Toast shows brief, non-modal notifications. This record covers the toast card (`Toast`), the viewport that stacks, hosts, and announces toasts (`ToastViewport`, which has no consumer doc of its own), and the behavior the -`useToast` show function drives. Consumer usage lives in `Toast.doc.mjs`. +`useToast` show function drives. Consumer usage lives in `Toast.doc.mjs` (the +card) and `useToast.doc.mjs` (the show function). ## Compatibility and migration diff --git a/packages/core/src/AlertDialog/useImperativeAlertDialog.doc.mjs b/packages/core/src/AlertDialog/useImperativeAlertDialog.doc.mjs index 8b560d5ba5869..c0dcfb546ef54 100644 --- a/packages/core/src/AlertDialog/useImperativeAlertDialog.doc.mjs +++ b/packages/core/src/AlertDialog/useImperativeAlertDialog.doc.mjs @@ -6,12 +6,14 @@ export const docs = { name: 'useImperativeAlertDialog', subComponentOf: 'AlertDialog', displayName: 'useImperativeAlertDialog', - description: 'Hook for showing an alert dialog without managing open state. Call alert.show(options) to open and alert.hide() to close. Render alert.element in your JSX tree.', + description: + 'Hook for showing an alert dialog without managing open state. Call alert.show(options) to open and alert.hide() to close. Render alert.element in your JSX tree.', props: [ { name: 'show', - type: '(options: AlertDialogOptions) => void', - description: 'Show the alert dialog with the given options. Options are the same as AlertDialog props minus isOpen/onOpenChange.', + type: "(options: Omit) => void", + description: + 'Show the alert dialog with the given options. Options are the same as AlertDialog props minus isOpen/onOpenChange.', }, { name: 'hide', @@ -33,7 +35,8 @@ export const docs = { export const docsDense = { name: 'useImperativeAlertDialog', - description: 'Hook to show alert dialog w/o managing open state. Call alert.show(options) to open, alert.hide() to close; render alert.element in JSX tree.', + description: + 'Hook to show alert dialog w/o managing open state. Call alert.show(options) to open, alert.hide() to close; render alert.element in JSX tree.', propDescriptions: { show: 'show dialog w/ options; same as AlertDialog props minus isOpen/onOpenChange', hide: 'hide dialog', diff --git a/packages/core/src/Dialog/useImperativeDialog.doc.mjs b/packages/core/src/Dialog/useImperativeDialog.doc.mjs index e79a94ee4506c..c93b54e0195e9 100644 --- a/packages/core/src/Dialog/useImperativeDialog.doc.mjs +++ b/packages/core/src/Dialog/useImperativeDialog.doc.mjs @@ -6,12 +6,14 @@ export const docs = { name: 'useImperativeDialog', subComponentOf: 'Dialog', displayName: 'useImperativeDialog', - description: 'Hook for showing a dialog without managing open state. Call dialog.show(content, options) to open and dialog.hide() to close. Render dialog.element in your JSX tree.', + description: + 'Hook for showing a dialog without managing open state. Call dialog.show(content, options) to open and dialog.hide() to close. Render dialog.element in your JSX tree.', props: [ { name: 'show', - type: '(content: ReactNode, options?: DialogOptions) => void', - description: 'Show the dialog with the given content. Options are the same as Dialog props minus isOpen/onOpenChange/children.', + type: "(content: ReactNode, options?: Omit) => void", + description: + 'Show the dialog with the given content. Options are the same as Dialog props minus isOpen/onOpenChange/children.', }, { name: 'hide', @@ -33,7 +35,8 @@ export const docs = { export const docsDense = { name: 'useImperativeDialog', - description: 'hook to show a dialog w/o managing open state; call dialog.show(content, options) to open + dialog.hide() to close; render dialog.element in your JSX tree', + description: + 'hook to show a dialog w/o managing open state; call dialog.show(content, options) to open + dialog.hide() to close; render dialog.element in your JSX tree', propDescriptions: { show: 'show the dialog with given content; options = Dialog props minus isOpen/onOpenChange/children', hide: 'hide the dialog', diff --git a/packages/core/src/Selector/Selector.doc.mjs b/packages/core/src/Selector/Selector.doc.mjs index 089442de184b4..fea6924b687b9 100644 --- a/packages/core/src/Selector/Selector.doc.mjs +++ b/packages/core/src/Selector/Selector.doc.mjs @@ -182,7 +182,7 @@ export const docs = { }, { name: 'options', - type: 'SelectorOption[]', + type: 'SelectorOptionType[]', description: 'Array of items: strings, objects with value/label/description/icon/disabled, dividers ({type: "divider"}), or sections ({type: "section", title, options}).', required: true, diff --git a/packages/core/src/theme/SyntaxTheme.doc.mjs b/packages/core/src/theme/SyntaxTheme.doc.mjs index f2b5baeb82551..ef32688f8e9cf 100644 --- a/packages/core/src/theme/SyntaxTheme.doc.mjs +++ b/packages/core/src/theme/SyntaxTheme.doc.mjs @@ -59,7 +59,7 @@ export const docs = { props: [ { name: 'theme', - type: 'SyntaxTheme', + type: 'SyntaxThemeDefinition', required: true, description: 'Syntax highlighting theme: a preset from @astryxdesign/core/theme/syntax or a custom theme created with defineSyntaxTheme().', diff --git a/packages/native/src/components/Selector.ts b/packages/native/src/components/Selector.ts index e2b3cbc638d28..7b8227e94153e 100644 --- a/packages/native/src/components/Selector.ts +++ b/packages/native/src/components/Selector.ts @@ -13,7 +13,7 @@ import type {NativeAdditions} from '../contract/fromComponentDoc.js'; export const selector: NativeAdditions = { name: 'Selector', baseTarget: 'trigger', - typeNames: {SelectorOption: 'SelectorOption'}, + typeNames: {SelectorOptionType: 'SelectorOption'}, types: { // The portable part of core's option data. Its rich description, icon, // and secondary control are web-only until typed slots exist.