Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/ui/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
26 changes: 1 addition & 25 deletions packages/ui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading