Skip to content

Commit 313cd7a

Browse files
committed
migrate jsx attribute refs and attribute directives
1 parent b389663 commit 313cd7a

4 files changed

Lines changed: 140 additions & 57 deletions

File tree

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,50 @@
11
---
22
title: "attr:*"
33
use_cases: >-
4-
web components, custom elements, forcing attributes, html attributes, dom
5-
properties
4+
custom elements, forcing attribute assignment, writing HTML attributes
65
tags:
76
- attributes
8-
- web-components
97
- dom
10-
- props
11-
- typescript
128
- custom-elements
139
version: "1.0"
1410
description: >-
15-
Force props as HTML attributes instead of properties in SolidJS. Essential for
16-
Web Components and custom element attribute handling.
11+
Force a JSX key to be written as an attribute instead of a property.
1712
---
1813

19-
Forces the prop to be treated as an attribute instead of a property.
20-
Useful for Web Components where you want to set attributes.
21-
22-
```tsx
23-
<my-element attr:status={props.status} />
24-
```
14+
`attr:*` forces a JSX key to be written as an attribute instead of a property.
2515

2616
:::note[Strong-Typing Custom Attributes]
2717
Type definitions are required when using TypeScript.
2818
See the[TypeScript](/configuration/typescript#forcing-properties-and-custom-attributes) page for examples.
2919
:::
20+
21+
## Syntax
22+
23+
```tsx
24+
<my-element attr:status={value} />
25+
```
26+
27+
## Value
28+
29+
- **Type:** attribute value
30+
31+
Value written to the attribute after the `attr:` prefix is removed.
32+
33+
## Behavior
34+
35+
- `attr:name={value}` writes the value to the `name` attribute.
36+
- The `attr:` prefix is removed before the attribute is written.
37+
- This is useful when an element expects data through attributes rather than DOM properties.
38+
39+
## Examples
40+
41+
### Basic usage
42+
43+
```tsx
44+
<my-element attr:status={props.status} />
45+
```
46+
47+
## Related
48+
49+
- [`prop:*`](/reference/jsx-attributes/prop)
50+
- [`bool:*`](/reference/jsx-attributes/bool)
Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,59 @@
11
---
22
title: "bool:*"
33
use_cases: >-
4-
web components, custom elements, conditional attributes, boolean attributes,
5-
dynamic attribute presence
4+
boolean attributes, custom elements, controlling attribute presence
65
tags:
76
- attributes
8-
- web-components
97
- boolean
10-
- conditional
11-
- dynamic
8+
- custom-elements
129
version: "1.0"
1310
description: >-
14-
Control attribute presence with bool:* directive in SolidJS. Add or remove
15-
attributes dynamically based on truthy/falsy values for web components.
11+
Control whether an attribute is present by writing through `bool:*`.
1612
---
1713

18-
`bool:*` controls the presence of an attribute in an element.
19-
When the value is `truthy` it adds the `attribute` to the element.
20-
Alternatively, when the value is `falsy` it removes the `attribute` from the element.
21-
This attribute is most useful for Web Components.
14+
`bool:*` controls whether an attribute is present on an element.
15+
16+
:::note[Strong-Typing Custom Boolean Attributes]
17+
Type definitions are required when using TypeScript.
18+
See the [TypeScript](/configuration/typescript#forcing-properties-and-custom-attributes) page for examples.
19+
:::
20+
21+
## Syntax
2222

2323
```tsx
24-
<my-element bool:status={prop.value} />
24+
<my-element bool:status={value} />
2525
```
2626

27+
## Value
28+
29+
- **Type:** any truthy or falsy value
30+
31+
Truthy values add the attribute. Falsy values remove it.
32+
33+
## Behavior
34+
35+
- `bool:name={value}` adds `name` when `value` is truthy.
36+
- `bool:name={value}` removes `name` when `value` is falsy.
37+
- When present, the attribute is written as an empty string attribute.
38+
39+
## Examples
40+
41+
### Basic usage
42+
43+
```tsx
44+
<my-element bool:status={props.value} />
45+
```
46+
47+
### Resulting markup
48+
2749
```tsx
28-
// Assuming `prop.value` is `truthy`, then it becomes
50+
// props.value is truthy
2951
<my-element status />
3052

31-
// And when `falsy`, then it becomes
53+
// props.value is falsy
3254
<my-element />
33-
3455
```
3556

36-
:::note[Strong-Typing Custom Boolean Attributes]
37-
Type definitions are required when using TypeScript.
38-
See the [TypeScript](/configuration/typescript#forcing-properties-and-custom-attributes) page for examples.
39-
:::
57+
## Related
58+
59+
- [`attr:*`](/reference/jsx-attributes/attr)

src/routes/reference/jsx-attributes/prop.mdx

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,50 @@
22
title: "prop:*"
33
order: 6
44
use_cases: >-
5-
dom properties, scrolltop manipulation, custom properties, property vs
6-
attribute, direct property access
5+
forcing property assignment, writing DOM properties, custom element property
6+
access
77
tags:
88
- properties
99
- dom
1010
- attributes
11-
- manipulation
12-
- custom
1311
version: "1.0"
1412
description: >-
15-
Force DOM property assignment with prop:* in SolidJS. Set properties directly
16-
instead of attributes for specific DOM manipulation needs.
13+
Force a JSX key to be written as a DOM property instead of an attribute.
1714
---
1815

19-
Forces the prop to be treated as a property instead of an attribute.
20-
21-
```tsx
22-
<div prop:scrollTop={props.scrollPos + "px"} />
23-
```
16+
`prop:*` forces a JSX key to be written as a DOM property instead of an attribute.
2417

2518
:::note[Strong-Typing Custom Properties]
2619
Type definitions are required when using TypeScript.
2720
See the [TypeScript](/configuration/typescript#forcing-properties-and-custom-attributes) page for examples.
2821
:::
22+
23+
## Syntax
24+
25+
```tsx
26+
<div prop:scrollTop={value} />
27+
```
28+
29+
## Value
30+
31+
- **Type:** property value
32+
33+
Value assigned to the property after the `prop:` prefix is removed.
34+
35+
## Behavior
36+
37+
- `prop:name={value}` assigns the value to the `name` property.
38+
- The `prop:` prefix is removed before the assignment happens.
39+
- This is useful when an element expects data through properties rather than attributes.
40+
41+
## Examples
42+
43+
### Basic usage
44+
45+
```tsx
46+
<div prop:scrollTop={props.scrollPos} />
47+
```
48+
49+
## Related
50+
51+
- [`attr:*`](/reference/jsx-attributes/attr)

src/routes/reference/jsx-attributes/ref.mdx

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,56 @@
22
title: ref
33
order: 7
44
use_cases: >-
5-
dom access, element manipulation, focus management, measurements, third-party
6-
libraries, animations
5+
element references, imperative DOM access, component refs
76
tags:
87
- refs
98
- dom
109
- elements
11-
- access
12-
- manipulation
13-
- components
1410
version: "1.0"
1511
description: >-
16-
Access DOM elements directly in SolidJS with refs. Get references to rendered
17-
elements for imperative operations and third-party integrations.
12+
Capture a rendered element or forwarded component ref.
1813
---
1914

20-
Refs are a way of getting access to underlying DOM elements in our JSX. While it is true one could just assign an element to a variable, it is more optimal to leave components in the flow of JSX. Refs are assigned at render time but before the elements are connected to the DOM. They come in 2 flavors.
15+
`ref` captures a rendered element or forwarded component ref.
16+
17+
## Syntax
18+
19+
```tsx
20+
<div ref={value} />
21+
```
22+
23+
## Value
24+
25+
- **Type:** variable binding or callback function
26+
27+
Receives the rendered element or forwarded ref value.
28+
29+
## Behavior
30+
31+
- Refs are assigned during rendering before the element is connected to the DOM.
32+
- A variable ref assigns the rendered element to the referenced variable.
33+
- A callback ref is called with the rendered element.
34+
- Component refs work only when the component forwards the `ref` prop to an underlying element or child component.
35+
36+
## Examples
37+
38+
### Variable ref
2139

2240
```tsx
23-
// variable assigned directly by ref
2441
let myDiv;
2542

26-
// use onMount or createEffect to read after connected to the DOM
2743
onMount(() => console.log(myDiv));
2844

29-
<div ref={myDiv} />
45+
<div ref={myDiv} />;
46+
```
47+
48+
### Callback ref
3049

31-
// Or, callback function (called before connected to the DOM)
32-
<div ref={el => console.log(el)} />
50+
```tsx
51+
<div ref={(el) => console.log(el)} />
3352
```
3453

35-
Refs can also be used on Components. They still need to be attached on the other side.
54+
### Component ref
3655

3756
```tsx
3857
function MyComp(props) {

0 commit comments

Comments
 (0)