Skip to content

Commit 86dd16c

Browse files
GitRowinRich HarrisRich-Harris
authored
feat: allow link options to be set to true and false (#10039)
* feat: allow link options to be set to true and false * update docs * Update .changeset/tricky-laws-camp.md --------- Co-authored-by: Rich Harris <git@rich-harris.dev> Co-authored-by: Rich Harris <hello@rich-harris.dev>
1 parent 51f3e66 commit 86dd16c

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

.changeset/tricky-laws-camp.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': minor
3+
---
4+
5+
feat: allow link options to be set to `"true"` and `"false"`

documentation/docs/30-advanced/30-link-options.md

+5-7
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ In certain cases, you may wish to disable this behaviour. Adding a `data-sveltek
104104

105105
## Disabling options
106106

107-
To disable any of these options inside an element where they have been enabled, use the `"off"` value:
107+
To disable any of these options inside an element where they have been enabled, use the `"false"` value:
108108

109109
```html
110110
<div data-sveltekit-preload-data>
@@ -113,7 +113,7 @@ To disable any of these options inside an element where they have been enabled,
113113
<a href="/b">b</a>
114114
<a href="/c">c</a>
115115

116-
<div data-sveltekit-preload-data="off">
116+
<div data-sveltekit-preload-data="false">
117117
<!-- these links will NOT be preloaded -->
118118
<a href="/d">d</a>
119119
<a href="/e">e</a>
@@ -122,10 +122,8 @@ To disable any of these options inside an element where they have been enabled,
122122
</div>
123123
```
124124

125-
To apply an attribute to an element conditionally, do this:
125+
To apply an attribute to an element conditionally, do this (`"true"` and `"false"` are both accepted values):
126126

127127
```html
128-
<div data-sveltekit-reload={shouldReload ? '' : 'off'}>
129-
```
130-
131-
> This works because in HTML, `<element attribute>` is equivalent to `<element attribute="">`
128+
<div data-sveltekit-reload={shouldReload}>
129+
```

packages/kit/src/runtime/client/utils.js

+22-8
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ const warned = new WeakSet();
3232
const valid_link_options = /** @type {const} */ ({
3333
'preload-code': ['', 'off', 'tap', 'hover', 'viewport', 'eager'],
3434
'preload-data': ['', 'off', 'tap', 'hover'],
35-
keepfocus: ['', 'off'],
36-
noscroll: ['', 'off'],
37-
reload: ['', 'off'],
38-
replacestate: ['', 'off']
35+
keepfocus: ['', 'true', 'off', 'false'],
36+
noscroll: ['', 'true', 'off', 'false'],
37+
reload: ['', 'true', 'off', 'false'],
38+
replacestate: ['', 'true', 'off', 'false']
3939
});
4040

4141
/**
@@ -176,13 +176,27 @@ export function get_router_options(element) {
176176
el = /** @type {Element} */ (parent_element(el));
177177
}
178178

179+
/** @param {string | null} value */
180+
function get_option_state(value) {
181+
switch (value) {
182+
case '':
183+
case 'true':
184+
return true;
185+
case 'off':
186+
case 'false':
187+
return false;
188+
default:
189+
return null;
190+
}
191+
}
192+
179193
return {
180194
preload_code: levels[preload_code ?? 'off'],
181195
preload_data: levels[preload_data ?? 'off'],
182-
keep_focus: keep_focus === 'off' ? false : keep_focus === '' ? true : null,
183-
noscroll: noscroll === 'off' ? false : noscroll === '' ? true : null,
184-
reload: reload === 'off' ? false : reload === '' ? true : null,
185-
replace_state: replace_state === 'off' ? false : replace_state === '' ? true : null
196+
keep_focus: get_option_state(keep_focus),
197+
noscroll: get_option_state(noscroll),
198+
reload: get_option_state(reload),
199+
replace_state: get_option_state(replace_state)
186200
};
187201
}
188202

0 commit comments

Comments
 (0)