From 5118602ffe585ac7c8e37cb7c6b2f1cf71d1e0b8 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Wed, 26 Aug 2026 00:05:32 +0200 Subject: [PATCH] UI: Move controlled prop guidance to contributing docs --- packages/ui/CONTRIBUTING.md | 28 ++++++++++++++++++++++++++++ packages/ui/README.md | 26 +------------------------- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/packages/ui/CONTRIBUTING.md b/packages/ui/CONTRIBUTING.md index 0c150fa331cf3e..6fe7311056277a 100644 --- a/packages/ui/CONTRIBUTING.md +++ b/packages/ui/CONTRIBUTING.md @@ -53,6 +53,34 @@ The package follows [semantic versioning](https://semver.org/), and the followin - Component props (e.g. renaming, removing, or changing a props supported types such that existing usage would break in an update) - CSS properties prefixed with `--wp-ui-` (e.g. changing a CSS property such that it would negatively impact a user's experience) +### Controlled and uncontrolled props + +When designing props for a new component: + +- Always offer both controlled and uncontrolled modes when the component has user-facing state. +- Name the uncontrolled prop `defaultX`, the controlled prop `x`, and the callback `onXChange`. +- In JSDoc comments, indicate which mode each prop is for and cross-reference the alternative: + + ```ts + /** + * Whether the panel is currently open (controlled). + * + * To render an uncontrolled component, use the `defaultOpen` prop instead. + */ + open?: boolean; + /** + * Whether the panel is initially open (uncontrolled). + * @default false + */ + defaultOpen?: boolean; + /** + * Event handler called when the open state changes. + */ + onOpenChange?: ( open: boolean ) => void; + ``` + +- Provide a `@default` JSDoc tag for the uncontrolled prop when there is a sensible default. + ## Compound Components This package follows the [compound component approach outlined in the `@wordpress/components` contributing guidelines](https://github.com/WordPress/gutenberg/blob/trunk/packages/components/CONTRIBUTING.md#compound-components). diff --git a/packages/ui/README.md b/packages/ui/README.md index c326b9edabc883..1a8b69118f0637 100644 --- a/packages/ui/README.md +++ b/packages/ui/README.md @@ -231,31 +231,7 @@ The `onXChange` callback is distinct from the native DOM `onChange` event handle Components that wrap native form elements may still support native event handlers (like `onChange`, `onInput`) for interoperability, but `onXChange` is the recommended approach within this package. -#### Guidelines for component authors - -When designing props for a new component: - -- Always offer both controlled and uncontrolled modes when the component has user-facing state. -- Name the uncontrolled prop `defaultX`, the controlled prop `x`, and the callback `onXChange`. -- In JSDoc comments, indicate which mode each prop is for and cross-reference the alternative: - ```ts - /** - * Whether the panel is currently open (controlled). - * - * To render an uncontrolled component, use the `defaultOpen` prop instead. - */ - open?: boolean; - /** - * Whether the panel is initially open (uncontrolled). - * @default false - */ - defaultOpen?: boolean; - /** - * Event handler called when the open state changes. - */ - onOpenChange?: ( open: boolean ) => void; - ``` -- Provide a `@default` JSDoc tag for the uncontrolled prop when there is a sensible default. +For guidance on implementing this pattern in new components, see [Controlled and uncontrolled props](./CONTRIBUTING.md#controlled-and-uncontrolled-props). ## Contributing to this package