Skip to content

Commit 6240128

Browse files
committed
Document dynamic prop labels in website docs
Add "Dynamic attribute names" section to elements.mdx and a brief mention with link in introduction.mdx. Includes translations for ja, zh-Hans, and zh-Hant locales.
1 parent 5d20965 commit 6240128

8 files changed

Lines changed: 256 additions & 0 deletions

File tree

website/docs/concepts/html/elements.mdx

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,59 @@ html! {
6060
};
6161
```
6262

63+
## Dynamic attribute names
64+
65+
Similar to dynamic tag names, attribute names can also be determined at runtime. This is useful for
66+
libraries like HTMX that use attributes with colons (e.g. `hx-on:click`), or any situation where
67+
the attribute name is not known at compile time.
68+
69+
There are two syntaxes for dynamic attribute names: string literals and expressions.
70+
71+
### String literal attribute names
72+
73+
Use a string literal directly as the attribute name:
74+
75+
```rust
76+
use yew::prelude::*;
77+
78+
html! {
79+
<div "hx-on:click"="alert('Clicked!')" />
80+
};
81+
```
82+
83+
This also works with braced values:
84+
85+
```rust
86+
use yew::prelude::*;
87+
88+
let handler = "alert('Clicked!')";
89+
90+
html! {
91+
<div "hx-on:click"={handler} />
92+
};
93+
```
94+
95+
### Expression attribute names
96+
97+
Wrap any expression in braces to compute the attribute name dynamically:
98+
99+
```rust
100+
use yew::prelude::*;
101+
102+
let attr_name = format!("hx-on:{}", "click");
103+
104+
html! {
105+
<div { attr_name }={ "alert('Clicked!')" } />
106+
};
107+
```
108+
109+
The expression must evaluate to a type that implements `Into<AttrValue>` (e.g. `String`, `&str`, `AttrValue`).
110+
111+
:::note
112+
Dynamic attribute names are only supported on HTML elements. Component properties require
113+
static Rust identifiers. See [Component Properties](../function-components/properties.mdx).
114+
:::
115+
63116
## Boolean Attributes
64117

65118
Some content attributes (e.g checked, hidden, required) are called boolean attributes. In Yew,

website/docs/concepts/html/introduction.mdx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,20 @@ and thus must be enclosed in curly-braces `{-6}`
184184

185185
:::
186186

187+
Attribute names can also be set dynamically using string literals or expressions. This is useful for
188+
attributes that are not valid Rust identifiers, such as `hx-on:click`:
189+
190+
```rust
191+
use yew::prelude::*;
192+
193+
html! { <div "hx-on:click"="alert('Clicked!')" /> };
194+
html! { <div { String::from("hx-on:click") }={ "alert('Clicked!')" } /> };
195+
```
196+
197+
:::info
198+
Read more at [Dynamic attribute names](./html/elements#dynamic-attribute-names)
199+
:::
200+
187201
:::note Component properties
188202
Component properties are passed as Rust objects and are different from the element attributes/properties described here.
189203
Read more about them at [Component Properties](../function-components/properties.mdx)

website/i18n/ja/docusaurus-plugin-content-docs/current/concepts/html/elements.mdx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,56 @@ html! {
5555
};
5656
```
5757

58+
## 動的な属性名
59+
60+
動的なタグ名と同様に、属性名も実行時に決定できます。これは HTMX のようなコロンを含む属性(例: `hx-on:click`)を使用するライブラリや、属性名がコンパイル時に不明な場合に便利です。
61+
62+
動的な属性名には、文字列リテラルと式の 2 つの構文があります。
63+
64+
### 文字列リテラルの属性名
65+
66+
文字列リテラルを属性名として直接使用します:
67+
68+
```rust
69+
use yew::prelude::*;
70+
71+
html! {
72+
<div "hx-on:click"="alert('Clicked!')" />
73+
};
74+
```
75+
76+
中括弧で囲んだ値でも使用できます:
77+
78+
```rust
79+
use yew::prelude::*;
80+
81+
let handler = "alert('Clicked!')";
82+
83+
html! {
84+
<div "hx-on:click"={handler} />
85+
};
86+
```
87+
88+
### 式の属性名
89+
90+
任意の式を中括弧で囲んで、属性名を動的に計算します:
91+
92+
```rust
93+
use yew::prelude::*;
94+
95+
let attr_name = format!("hx-on:{}", "click");
96+
97+
html! {
98+
<div { attr_name }={ "alert('Clicked!')" } />
99+
};
100+
```
101+
102+
式は `Into<AttrValue>` を実装する型(例: `String``&str``AttrValue`)に評価される必要があります。
103+
104+
:::note
105+
動的な属性名は HTML 要素でのみサポートされています。コンポーネントのプロパティには静的な Rust 識別子が必要です。[コンポーネントプロパティ](../function-components/properties.mdx)を参照してください。
106+
:::
107+
58108
## 論理値属性
59109

60110
いくつかのコンテンツ属性(例えば、checked、hidden、required)は論理値属性と呼ばれます。Yew では、論理値属性はブール値に設定する必要があります:

website/i18n/ja/docusaurus-plugin-content-docs/current/concepts/html/introduction.mdx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,19 @@ html! { <my-element ~property="abc" /> };
172172

173173
:::
174174

175+
属性名は文字列リテラルや式を使用して動的に設定することもできます。これは `hx-on:click` のような有効な Rust 識別子ではない属性に便利です:
176+
177+
```rust
178+
use yew::prelude::*;
179+
180+
html! { <div "hx-on:click"="alert('Clicked!')" /> };
181+
html! { <div { String::from("hx-on:click") }={ "alert('Clicked!')" } /> };
182+
```
183+
184+
:::info
185+
詳細は[動的な属性名](./html/elements#dynamic-attribute-names)を参照してください。
186+
:::
187+
175188
:::note コンポーネント属性
176189
コンポーネント属性は Rust オブジェクトとして渡され、ここで説明されている要素のパラメータ (Attributes) / 属性 (Properties) とは異なります。
177190
[コンポーネント属性](../function-components/properties.mdx)で詳細を確認してください。

website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/concepts/html/elements.mdx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,56 @@ html! {
5555
};
5656
```
5757

58+
## 动态属性名
59+
60+
与动态标签名类似,属性名也可以在运行时确定。这对于使用包含冒号的属性(例如 `hx-on:click`)的 HTMX 等库,或属性名在编译时未知的任何情况都很有用。
61+
62+
动态属性名有两种语法:字符串字面量和表达式。
63+
64+
### 字符串字面量属性名
65+
66+
直接使用字符串字面量作为属性名:
67+
68+
```rust
69+
use yew::prelude::*;
70+
71+
html! {
72+
<div "hx-on:click"="alert('Clicked!')" />
73+
};
74+
```
75+
76+
也可以使用大括号包裹的值:
77+
78+
```rust
79+
use yew::prelude::*;
80+
81+
let handler = "alert('Clicked!')";
82+
83+
html! {
84+
<div "hx-on:click"={handler} />
85+
};
86+
```
87+
88+
### 表达式属性名
89+
90+
将任意表达式用大括号包裹来动态计算属性名:
91+
92+
```rust
93+
use yew::prelude::*;
94+
95+
let attr_name = format!("hx-on:{}", "click");
96+
97+
html! {
98+
<div { attr_name }={ "alert('Clicked!')" } />
99+
};
100+
```
101+
102+
表达式必须计算为实现了 `Into<AttrValue>` 的类型(例如 `String``&str``AttrValue`)。
103+
104+
:::note
105+
动态属性名仅支持 HTML 元素。组件属性需要静态的 Rust 标识符。请参阅[组件属性](../function-components/properties.mdx)
106+
:::
107+
58108
## 逻辑值属性
59109

60110
一些内容属性(例如 checked、hidden、required)被称为逻辑值属性。在 Yew 中,逻辑值属性需要设置为布尔值:

website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/concepts/html/introduction.mdx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,19 @@ html! { <my-element ~property="abc" /> };
172172

173173
:::
174174

175+
属性名也可以使用字符串字面量或表达式动态设置。这对于不是有效 Rust 标识符的属性(如 `hx-on:click`)很有用:
176+
177+
```rust
178+
use yew::prelude::*;
179+
180+
html! { <div "hx-on:click"="alert('Clicked!')" /> };
181+
html! { <div { String::from("hx-on:click") }={ "alert('Clicked!')" } /> };
182+
```
183+
184+
:::info
185+
详情请参阅[动态属性名](./html/elements#dynamic-attribute-names)
186+
:::
187+
175188
:::note 组件属性
176189
组件属性作为 Rust 对象传递,与此处描述的元素参数 (Attributes) / 属性 (Properties) 不同。
177190
[组件属性](../function-components/properties.mdx)中了解更多信息。

website/i18n/zh-Hant/docusaurus-plugin-content-docs/current/concepts/html/elements.mdx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,56 @@ html! {
5555
};
5656
```
5757

58+
## 動態屬性名
59+
60+
與動態標籤名類似,屬性名也可以在執行時確定。這對於使用包含冒號的屬性(例如 `hx-on:click`)的 HTMX 等函式庫,或屬性名在編譯時未知的任何情況都很有用。
61+
62+
動態屬性名有兩種語法:字串字面量和表達式。
63+
64+
### 字串字面量屬性名
65+
66+
直接使用字串字面量作為屬性名:
67+
68+
```rust
69+
use yew::prelude::*;
70+
71+
html! {
72+
<div "hx-on:click"="alert('Clicked!')" />
73+
};
74+
```
75+
76+
也可以使用大括號包裹的值:
77+
78+
```rust
79+
use yew::prelude::*;
80+
81+
let handler = "alert('Clicked!')";
82+
83+
html! {
84+
<div "hx-on:click"={handler} />
85+
};
86+
```
87+
88+
### 表達式屬性名
89+
90+
將任意表達式用大括號包裹來動態計算屬性名:
91+
92+
```rust
93+
use yew::prelude::*;
94+
95+
let attr_name = format!("hx-on:{}", "click");
96+
97+
html! {
98+
<div { attr_name }={ "alert('Clicked!')" } />
99+
};
100+
```
101+
102+
表達式必須計算為實現了 `Into<AttrValue>` 的型別(例如 `String``&str``AttrValue`)。
103+
104+
:::note
105+
動態屬性名僅支援 HTML 元素。元件屬性需要靜態的 Rust 識別符號。請參閱[元件屬性](../function-components/properties.mdx)
106+
:::
107+
58108
## 邏輯值屬性
59109

60110
一些內容屬性(例如 checked、hidden、required)被稱為邏輯值屬性。在 Yew 中,邏輯值屬性需要設定為布林值:

website/i18n/zh-Hant/docusaurus-plugin-content-docs/current/concepts/html/introduction.mdx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,19 @@ html! { <my-element ~property="abc" /> };
172172

173173
:::
174174

175+
屬性名也可以使用字串字面量或表達式動態設定。這對於不是有效 Rust 識別符號的屬性(如 `hx-on:click`)很有用:
176+
177+
```rust
178+
use yew::prelude::*;
179+
180+
html! { <div "hx-on:click"="alert('Clicked!')" /> };
181+
html! { <div { String::from("hx-on:click") }={ "alert('Clicked!')" } /> };
182+
```
183+
184+
:::info
185+
詳情請參閱[動態屬性名](./html/elements#dynamic-attribute-names)
186+
:::
187+
175188
:::note 元件屬性
176189
元件屬性以 Rust 物件傳遞,與此處所述的元素參數 (Attributes) / 屬性 (Properties) 不同。
177190
[元件屬性](../function-components/properties.mdx)中了解更多。

0 commit comments

Comments
 (0)