From 687a3bff89f9688361cb28235c732c9a7dca4b24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ramirez=20Vargas=2C=20Jos=C3=A9=20Pablo?= Date: Wed, 19 Feb 2025 13:05:57 -0600 Subject: [PATCH] feat: Add the LinkContext component Closes FEATURE: Option to preserve query parameters #9 . --- src/lib/Link/Link.svelte | 176 ++++++++++++++----------- src/lib/Link/README.md | 1 + src/lib/LinkContext/LinkContext.svelte | 91 +++++++++++++ src/lib/LinkContext/README.md | 45 +++++++ src/lib/index.ts | 1 + 5 files changed, 236 insertions(+), 78 deletions(-) create mode 100644 src/lib/LinkContext/LinkContext.svelte create mode 100644 src/lib/LinkContext/README.md diff --git a/src/lib/Link/Link.svelte b/src/lib/Link/Link.svelte index 5c441ee..97bdb5b 100644 --- a/src/lib/Link/Link.svelte +++ b/src/lib/Link/Link.svelte @@ -1,108 +1,128 @@ + + + +{@render children?.()} diff --git a/src/lib/LinkContext/README.md b/src/lib/LinkContext/README.md new file mode 100644 index 0000000..f1b1fa2 --- /dev/null +++ b/src/lib/LinkContext/README.md @@ -0,0 +1,45 @@ +# LinkContext + +The `LinkContext` component is used to create context for `Link` components. This context can be used to set, in +mass, the `replace`, `prependBasePath` and `preserveQuery` properties. + +Instead of writing this: + +```svelte +... +... +... +... +``` + +You can do: + +```svelte + + ... + ... + ... + +``` + +Unlike the rest of components in this library, this one does not support the `hash` property. The context is +inherited by all links among its children. + +**Note**: The `preserveQuery` option only has an effect on path routing links since hash routing links should not +lose the query string. + +## Priorities + +The `Link` component will give priority to an explicitly-set value at its property level. If a property-level value is +not found, then the context-provided property value is used. If there is no context, then the default value takes over. + +## Props + +| Property | Type | Default Value | Bindable | Description | +|-|-|-|-|-| +| `replace` | `boolean` | `false` | | Configures the link so it replaces the current URL as opposed to pushing the URL as a new entry in the browser's History API. | +| `prependBasePath` | `boolean` | `false` | | Configures the component to prepend the parent router's base path to the `href` property. | +| `preserveQuery` | `boolean \| string \| string[]` | `false` | | Configures the component to preserve the query string whenever it triggers navigation. | +| `children` | `Snippet` | `undefined` | | Renders the children of the component. | + +[Online Documentation](https://wjfe-n-savant.hashnode.space/wjfe-n-savant/components/linkcontext) diff --git a/src/lib/index.ts b/src/lib/index.ts index b808b43..47cfb3c 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -42,6 +42,7 @@ export function init(options?: InitOptions): () => void { export * from "$lib/Link/Link.svelte"; export { default as Link } from "$lib/Link/Link.svelte"; +export { default as LinkContext } from "$lib/LinkContext/LinkContext.svelte"; export * from "$lib/Route/Route.svelte"; export { default as Route } from "$lib/Route/Route.svelte"; export * from "$lib/Router/Router.svelte";