diff --git a/documentation/docs/01-introduction/03-svelte-files.md b/documentation/docs/01-introduction/03-svelte-files.md new file mode 100644 index 0000000000..d68bb89126 --- /dev/null +++ b/documentation/docs/01-introduction/03-svelte-files.md @@ -0,0 +1,66 @@ +--- +title: .svelte files +--- + +Components are the building blocks of Svelte applications. They are written into `.svelte` files, using a superset of HTML. + +All three sections — script, styles and markup — are optional. + + +```svelte +/// file: MyComponent.svelte + + + + + + + +``` + +## ` + + +``` + +## ` +``` + +For more information regarding styling, read the documentation around [styles and classes](styles-and-classes). diff --git a/documentation/docs/01-introduction/04-svelte-js-files.md b/documentation/docs/01-introduction/04-svelte-js-files.md new file mode 100644 index 0000000000..d0dde34111 --- /dev/null +++ b/documentation/docs/01-introduction/04-svelte-js-files.md @@ -0,0 +1,7 @@ +--- +title: .svelte.js and .svelte.ts files +--- + +Besides `.svelte` files, Svelte also operates on `.svelte.js` and `.svelte.ts` files. + +These behave like any other `.js` or `.ts` module, except that you can use runes. This is useful for creating reusable reactive logic, or sharing reactive state across your app. diff --git a/documentation/docs/01-introduction/xx-props.md b/documentation/docs/01-introduction/xx-props.md new file mode 100644 index 0000000000..cad854d878 --- /dev/null +++ b/documentation/docs/01-introduction/xx-props.md @@ -0,0 +1,139 @@ +--- +title: Public API of a component +--- + +### Public API of a component + +Svelte uses the `$props` rune to declare _properties_ or _props_, which means describing the public interface of the component which becomes accessible to consumers of the component. + +> [!NOTE] `$props` is one of several runes, which are special hints for Svelte's compiler to make things reactive. + +```svelte + +``` + +You can specify a fallback value for a prop. It will be used if the component's consumer doesn't specify the prop on the component when instantiating the component, or if the passed value is `undefined` at some point. + +```svelte + +``` + +To get all properties, use rest syntax: + +```svelte + +``` + +You can use reserved words as prop names. + +```svelte + +``` + +If you're using TypeScript, you can declare the prop types: + +```svelte + +``` + +If you're using JavaScript, you can declare the prop types using JSDoc: + +```svelte + +``` + +If you export a `const`, `class` or `function`, it is readonly from outside the component. + +```svelte + +``` + +Readonly props can be accessed as properties on the element, tied to the component using [`bind:this` syntax](bindings#bind:this). + +### Reactive variables + +To change component state and trigger a re-render, just assign to a locally declared variable that was declared using the `$state` rune. + +Update expressions (`count += 1`) and property assignments (`obj.x = y`) have the same effect. + +```svelte + +``` + +Svelte's ` +``` + +If you'd like to react to changes to a prop, use the `$derived` or `$effect` runes instead. + +```svelte + +``` + +For more information on reactivity, read the documentation around runes. diff --git a/documentation/docs/01-introduction/03-reactivity-fundamentals.md b/documentation/docs/01-introduction/xx-reactivity-fundamentals.md similarity index 100% rename from documentation/docs/01-introduction/03-reactivity-fundamentals.md rename to documentation/docs/01-introduction/xx-reactivity-fundamentals.md