Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add useSettings hook for reading multiple settings at once #55337

Merged
merged 18 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Update docs
  • Loading branch information
jsnajdr committed Oct 19, 2023
commit 2205c386d5d48a15ff5b38aaa4781d9cdaeab89a
6 changes: 6 additions & 0 deletions packages/block-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,12 @@ Hook that retrieves the given settings for the block instance in use.

It looks up the settings first in the block instance hierarchy. If none are found, it'll look them up in the block editor settings.

_Usage_

```js
const [ fixed, sticky ] = useSettings( 'position.fixed', 'position.sticky' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused here because the PR description says

const [ allowFixed, allowSticky ] = useSettings( [ 'position.fixed', 'position.sticky' ] );

Can both be used? It would be nice to be consistent so we don't need to normalise.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only the useSettings( 'a', 'b' ) form can be used. The PR originally started with an array argument, useSettings( settings: string[] ), but then was changed to useSettings( ...settings: string[] ) during review. That's why the PR description is out of date.

The main reason for that was that when reading a single setting, useSetting( 'a' ) and useSettings( 'a' ) are the same, except that for useSettings the return value is always an array (with one element).

```

_Parameters_

- _paths_ `string[]`: The paths to the settings.
Expand Down
19 changes: 9 additions & 10 deletions packages/block-editor/src/components/use-setting/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
## Use Setting
## Use Settings

`useSetting` is a hook that will retrive the setting for the block instance that's in use.
`useSettings` is a hook that will retrieve the settings for the block instance that's in use.

It does the lookup of the setting in the following order:
It does the lookup of the settings in the following order:

1. Third parties can provide the settings for the block using the filter `blockEditor.useSetting.before`.
2. If no third parties have provided this setting, then it looks up in the block instance hierachy starting from the current block and working its way upwards to its ancestors.
Expand All @@ -20,20 +20,19 @@ It does the lookup of the setting in the following order:
This will fetch the default color palette based on the block instance.

```jsx
import { useSetting } from '@wordpress/block-editor';
import { useSettings } from '@wordpress/block-editor';

const defaultColorPalette = useSetting( 'color.palette.default' );
const [ defaultColorPalette ] = useSettings( 'color.palette.default' );
```

Refer [here](https://github.com/WordPress/gutenberg/blob/HEAD/docs/how-to-guides/curating-the-editor-experience.md?plain=1#L330) in order to understand how the filter mentioned above `blockEditor.useSetting.before` can be used.

### Props

This hooks accepts the following props.
This hooks accepts the following arguments.

#### `path`
#### `paths`

- **Type:** `String`
- **Default:** `undefined`
- **Type:** `Array<String>`

The path to the setting that is to be used for a block. Ex: `typography.fontSizes`
Array of paths to the settings to be retrieved. E.g., `[ 'typography.fontSizes' ]`
4 changes: 4 additions & 0 deletions packages/block-editor/src/components/use-setting/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ function mergeOrigins( value ) {
*
* @param {string[]} paths The paths to the settings.
* @return {any[]} Returns the values defined for the settings.
* @example
* ```js
* const [ fixed, sticky ] = useSettings( 'position.fixed', 'position.sticky' );
* ```
*/
export function useSettings( ...paths ) {
const { name: blockName, clientId = null } = useBlockEditContext();
Expand Down