Skip to content

Commit

Permalink
Docs/iAPI: Recommend kebab-case in data-wp-class (#62817)
Browse files Browse the repository at this point in the history
* Recommend kebab-case in wp-class and remove outdated info about callback args

* Trigger actions again

Co-authored-by: luisherranz <[email protected]>
Co-authored-by: cbravobernal <[email protected]>
  • Loading branch information
3 people authored Jun 27, 2024
1 parent fe67010 commit b883f30
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions docs/reference-guides/interactivity-api/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,36 @@ The `wp-class` directive is executed:
- When the element is created
- Each time there's a change on any of the properties of the `state` or `context` involved in getting the final value of the directive (inside the callback or the expression passed as reference)

When `wp-class` directive references a callback to get its final boolean value, the callback receives the class name: `className`.

The boolean value received by the directive is used to toggle (add when `true` or remove when `false`) the associated class name from the `class` attribute.

It's important to note that when using the `wp-class` directive, it's recommended to use kebab-case for class names instead of camelCase. This is because HTML attributes are not case-sensitive, and HTML will treat `data-wp-class--isDark` the same as `data-wp-class--isdark` or `DATA-WP-CLASS--ISDARK`.

So, for example, use the class name `is-dark` instead of `isDark` and `data-wp-class--is-dark` instead of `data-wp-class--isDark`:

```html
<!-- Recommended -->
<div data-wp-class--is-dark="context.isDarkMode">
<!-- ... -->
</div>

<!-- Not recommended -->
<div data-wp-class--isDark="context.isDarkMode">
<!-- ... -->
</div>
```

```css
/* Recommended */
.is-dark {
/* ... */
}

/* Not recommended */
.isDark {
/* ... */
}
```

### `wp-style`

This directive adds or removes inline style to an HTML element, depending on its value. It follows the syntax `data-wp-style--css-property`.
Expand Down Expand Up @@ -256,8 +282,6 @@ The `wp-style` directive is executed:
- When the element is created
- Each time there's a change on any of the properties of the `state` or `context` involved in getting the final value of the directive (inside the callback or the expression passed as reference)

When `wp-style` directive references a callback to get its final value, the callback receives the class style property: `css-property`.

The value received by the directive is used to add or remove the style attribute with the associated CSS property:

- If the value is `false`, the style attribute is removed: `<div>`
Expand Down

1 comment on commit b883f30

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in b883f30.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/9700496194
📝 Reported issues:

Please sign in to comment.