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

Update class:list and astro-hash classes #4322

Merged
merged 4 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 3 additions & 3 deletions src/content/docs/en/guides/styling.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ This CSS:
Compiles to this:
```astro
<style>
h1:where(.astro-HHNQFKH6) {
h1:where(.astro-hhnqfkh6) {
natemoo-re marked this conversation as resolved.
Show resolved Hide resolved
color: red;
}

.text:where(.astro-HHNQFKH6) {
.text:where(.astro-hhnqfkh6) {
natemoo-re marked this conversation as resolved.
Show resolved Hide resolved
color: blue;
}
</style>
natemoo-re marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -159,7 +159,7 @@ import MyComponent from "../components/MyComponent.astro"
<MyComponent class="red">This will be red!</MyComponent>
```

This pattern lets you style child components directly. Astro will pass the parent’s scoped class name (e.g. `astro-HHNQFKH6`) through the `class` prop automatically, including the child in its parent’s scope.
This pattern lets you style child components directly. Astro will pass the parent’s scoped class name (e.g. `astro-hhnqfkh6`) through the `class` prop automatically, including the child in its parent’s scope.

:::note[Scoped classes from parent components]
Because the `class` prop includes the child in its parent’s scope, it is possible for styles to cascade from parent to child. To avoid this having unintended side effects, ensure you use unique class names in the child component.
Expand Down
9 changes: 3 additions & 6 deletions src/content/docs/en/reference/directives-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,21 @@ A template directive is never included directly in the final HTML output of a co
## Common Directives
### `class:list`

`class:list={...}` takes an array of class values and converts them into a class string. This is inspired by @lukeed's popular [clsx](https://github.com/lukeed/clsx) helper library, but built directly into Astro itself.
`class:list={...}` takes an array of class values and converts them into a class string. This is powered by @lukeed's popular [clsx](https://github.com/lukeed/clsx) helper library.

`class:list` takes an array of several different possible value kinds:
- `string`: Added to the element `class`
- `Object`: All truthy keys are added to the element `class`
- `Array`: flattened
- `Set`: flattened
- `false` or `null`: skipped
- `false`, `null`, or `undefined`: skipped

```astro
<!-- This -->
<span class:list={[ 'hello goodbye', { hello: true, world: true }, new Set([ 'hello', 'friend' ]) ]} />
<span class:list={[ 'hello goodbye', { world: true }, [ 'friend' ] ]} />
<!-- Becomes -->
<span class="hello goodbye world friend"></span>
```

Duplicate values are removed automatically.

### `set:html`

`set:html={string}` injects an HTML string into an element, similar to setting `el.innerHTML`.
Expand Down
Loading