From 86dd16cc10c35c4b723a4e7f77e654452aa0965e Mon Sep 17 00:00:00 2001 From: Rowin <20298291+GitRowin@users.noreply.github.com> Date: Fri, 26 May 2023 03:02:47 +0200 Subject: [PATCH] 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 Co-authored-by: Rich Harris --- .changeset/tricky-laws-camp.md | 5 ++++ .../docs/30-advanced/30-link-options.md | 12 ++++---- packages/kit/src/runtime/client/utils.js | 30 ++++++++++++++----- 3 files changed, 32 insertions(+), 15 deletions(-) create mode 100644 .changeset/tricky-laws-camp.md diff --git a/.changeset/tricky-laws-camp.md b/.changeset/tricky-laws-camp.md new file mode 100644 index 000000000000..9733c0f1c6ec --- /dev/null +++ b/.changeset/tricky-laws-camp.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': minor +--- + +feat: allow link options to be set to `"true"` and `"false"` diff --git a/documentation/docs/30-advanced/30-link-options.md b/documentation/docs/30-advanced/30-link-options.md index bfd309cee649..115429801aca 100644 --- a/documentation/docs/30-advanced/30-link-options.md +++ b/documentation/docs/30-advanced/30-link-options.md @@ -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
@@ -113,7 +113,7 @@ To disable any of these options inside an element where they have been enabled, b c -
+
d e @@ -122,10 +122,8 @@ To disable any of these options inside an element where they have been enabled,
``` -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 -
-``` - -> This works because in HTML, `` is equivalent to `` +
+``` \ No newline at end of file diff --git a/packages/kit/src/runtime/client/utils.js b/packages/kit/src/runtime/client/utils.js index 1ac2aeff9a6c..8af6102cd93f 100644 --- a/packages/kit/src/runtime/client/utils.js +++ b/packages/kit/src/runtime/client/utils.js @@ -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'] }); /** @@ -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) }; }