Skip to content

Commit

Permalink
feat: allow link options to be set to true and false (#10039)
Browse files Browse the repository at this point in the history
* feat: allow link options to be set to true and false

* update docs

* Update .changeset/tricky-laws-camp.md

---------

Co-authored-by: Rich Harris <[email protected]>
Co-authored-by: Rich Harris <[email protected]>
  • Loading branch information
3 people authored May 26, 2023
1 parent 51f3e66 commit 86dd16c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-laws-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': minor
---

feat: allow link options to be set to `"true"` and `"false"`
12 changes: 5 additions & 7 deletions documentation/docs/30-advanced/30-link-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ In certain cases, you may wish to disable this behaviour. Adding a `data-sveltek

## Disabling options

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

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

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

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

```html
<div data-sveltekit-reload={shouldReload ? '' : 'off'}>
```

> This works because in HTML, `<element attribute>` is equivalent to `<element attribute="">`
<div data-sveltekit-reload={shouldReload}>
```
30 changes: 22 additions & 8 deletions packages/kit/src/runtime/client/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ const warned = new WeakSet();
const valid_link_options = /** @type {const} */ ({
'preload-code': ['', 'off', 'tap', 'hover', 'viewport', 'eager'],
'preload-data': ['', 'off', 'tap', 'hover'],
keepfocus: ['', 'off'],
noscroll: ['', 'off'],
reload: ['', 'off'],
replacestate: ['', 'off']
keepfocus: ['', 'true', 'off', 'false'],
noscroll: ['', 'true', 'off', 'false'],
reload: ['', 'true', 'off', 'false'],
replacestate: ['', 'true', 'off', 'false']
});

/**
Expand Down Expand Up @@ -176,13 +176,27 @@ export function get_router_options(element) {
el = /** @type {Element} */ (parent_element(el));
}

/** @param {string | null} value */
function get_option_state(value) {
switch (value) {
case '':
case 'true':
return true;
case 'off':
case 'false':
return false;
default:
return null;
}
}

return {
preload_code: levels[preload_code ?? 'off'],
preload_data: levels[preload_data ?? 'off'],
keep_focus: keep_focus === 'off' ? false : keep_focus === '' ? true : null,
noscroll: noscroll === 'off' ? false : noscroll === '' ? true : null,
reload: reload === 'off' ? false : reload === '' ? true : null,
replace_state: replace_state === 'off' ? false : replace_state === '' ? true : null
keep_focus: get_option_state(keep_focus),
noscroll: get_option_state(noscroll),
reload: get_option_state(reload),
replace_state: get_option_state(replace_state)
};
}

Expand Down

0 comments on commit 86dd16c

Please sign in to comment.