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

docs: clarify default prop values, attribute behavior and text expressions #15257

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
22 changes: 22 additions & 0 deletions documentation/docs/01-introduction/xx-props.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@ You can specify a fallback value for a prop. It will be used if the component's
</script>
```

> [!NOTE] If a prop is explicitly passed as null, the default value will not be used, and null will be assigned instead. However, if the prop is undefined or not provided at all, the default value will be used.

```svelte
<script>
let { answer = 3 } = $props();
</script>

<p>The answer is {answer}</p>
```

```svelte
<script>
import Nested from './Nested.svelte';
</script>

<Nested answer={42} /> <!-- answer is set to 42 -->
<Nested answer={null} /> <!-- answer is set to null (default value is not used)-->
<Nested /> <!-- answer is set to default value -->
<Nested answer={undefined}/> <!-- answer is set to default value -->
Copy link

Choose a reason for hiding this comment

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

Maybe it's better to write exactly what value in both lines, for example, "answer is set to 3 (default value)"

Copy link

Choose a reason for hiding this comment

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

Add an example with deep nesting


```

To get all properties, use rest syntax:

```svelte
Expand Down
43 changes: 43 additions & 0 deletions documentation/docs/03-template-syntax/01-basic-markup.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,29 @@ When the attribute name and value match (`name={name}`), they can be replaced wi
-->
```

When passing null or undefined to an attribute, the attribute is omitted from the rendered HTML.

```svelte
<script>
let someId = undefined;
let someClass = null;
</script>

<div id={someId} class={someClass}>Attributes are not included.</div>
<!-- The 'id' and 'class' attributes won't be included in the rendered HTML -->
```

If an empty string ("") is assigned to an attribute, the attribute remains in the HTML but with an empty value.

```svelte
<script>
let emptyClass = ""
</script>

<div class={emptyClass}>Hello</div>
<!-- This will render as: <div class="">Hello</div> -->
```

## Component props

By convention, values passed to components are referred to as _properties_ or _props_ rather than _attributes_, which are a feature of the DOM.
Expand Down Expand Up @@ -154,6 +177,26 @@ A JavaScript expression can be included as text by surrounding it with curly bra
{expression}
```

When using {expression} inside markup, Svelte automatically converts the value to a string before rendering it. The conversion follows JavaScript's standard behavior:

- Primitive values (number, boolean, string) are directly converted to strings.
- Objects call their .toString() method (if not overridden, it defaults to [object Object]).
- undefined and null are treated as empty strings ("").

```svelte
<script>
let num = 1;
let bool = false;
let obj = { key: "value" };
let empty = undefined;
</script>

<p>{num}</p> <!-- Renders as: <p>1</p> -->
<p>{bool}</p> <!-- Renders as: <p>false</p> -->
<p>{obj}</p> <!-- Renders as: <p>[object Object]</p> -->
<p>{empty}</p> <!-- Renders as: <p></p> (empty string) -->
Copy link

Choose a reason for hiding this comment

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

Add an example with an empty string, null, and an object with the toString method.
Perhaps it's better to shorten the example by removing the script tag.

```

Curly braces can be included in a Svelte template by using their [HTML entity](https://developer.mozilla.org/docs/Glossary/Entity) strings: `&lbrace;`, `&lcub;`, or `&#123;` for `{` and `&rbrace;`, `&rcub;`, or `&#125;` for `}`.

If you're using a regular expression (`RegExp`) [literal notation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#literal_notation_and_constructor), you'll need to wrap it in parentheses.
Expand Down