Type Switch using $$Props
This commit is contained in:
@@ -1,3 +1,12 @@
|
|||||||
|
<script lang="ts" context="module">
|
||||||
|
type TSwitchProps<
|
||||||
|
TSlotProps extends {},
|
||||||
|
TAsProp extends SupportedAs
|
||||||
|
> = TPassThroughProps<TSlotProps, TAsProp> & {
|
||||||
|
checked: boolean;
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { useSwitchContext } from "./SwitchGroup.svelte";
|
import { useSwitchContext } from "./SwitchGroup.svelte";
|
||||||
import { useLabelContext } from "$lib/components/label/LabelProvider.svelte";
|
import { useLabelContext } from "$lib/components/label/LabelProvider.svelte";
|
||||||
@@ -9,18 +18,26 @@
|
|||||||
import { get_current_component } from "svelte/internal";
|
import { get_current_component } from "svelte/internal";
|
||||||
import type { SupportedAs } from "$lib/internal/elements";
|
import type { SupportedAs } from "$lib/internal/elements";
|
||||||
import type { HTMLActionArray } from "$lib/hooks/use-actions";
|
import type { HTMLActionArray } from "$lib/hooks/use-actions";
|
||||||
import Render from "$lib/utils/Render.svelte";
|
import Render, { type TPassThroughProps } from "$lib/utils/Render.svelte";
|
||||||
import { resolveButtonType } from "$lib/utils/resolve-button-type";
|
import { resolveButtonType } from "$lib/utils/resolve-button-type";
|
||||||
|
|
||||||
|
/***** Props *****/
|
||||||
|
type TAsProp = $$Generic<SupportedAs>;
|
||||||
|
type $$Props = TSwitchProps<typeof slotProps, TAsProp>;
|
||||||
|
|
||||||
|
export let as: SupportedAs = "button";
|
||||||
|
export let use: HTMLActionArray = [];
|
||||||
|
export let checked = false;
|
||||||
|
|
||||||
|
/***** Events *****/
|
||||||
const forwardEvents = forwardEventsBuilder(get_current_component(), [
|
const forwardEvents = forwardEventsBuilder(get_current_component(), [
|
||||||
"change",
|
"change",
|
||||||
]);
|
]);
|
||||||
export let as: SupportedAs = "button";
|
|
||||||
export let use: HTMLActionArray = [];
|
|
||||||
|
|
||||||
const dispatch = createEventDispatcher<{
|
const dispatch = createEventDispatcher<{
|
||||||
change: boolean;
|
change: boolean;
|
||||||
}>();
|
}>();
|
||||||
export let checked = false;
|
|
||||||
|
/***** Component *****/
|
||||||
let api = useSwitchContext();
|
let api = useSwitchContext();
|
||||||
let labelContext = useLabelContext();
|
let labelContext = useLabelContext();
|
||||||
let descriptionContext = useDescriptionContext();
|
let descriptionContext = useDescriptionContext();
|
||||||
|
|||||||
@@ -7,6 +7,11 @@
|
|||||||
export function useSwitchContext(): Writable<StateDefinition> | undefined {
|
export function useSwitchContext(): Writable<StateDefinition> | undefined {
|
||||||
return getContext(SWITCH_CONTEXT_NAME);
|
return getContext(SWITCH_CONTEXT_NAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TSwitchGroupProps<
|
||||||
|
TSlotProps extends {},
|
||||||
|
TAsProp extends SupportedAs
|
||||||
|
> = TPassThroughProps<TSlotProps, TAsProp> & {};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
@@ -19,11 +24,19 @@
|
|||||||
import { get_current_component } from "svelte/internal";
|
import { get_current_component } from "svelte/internal";
|
||||||
import type { SupportedAs } from "$lib/internal/elements";
|
import type { SupportedAs } from "$lib/internal/elements";
|
||||||
import type { HTMLActionArray } from "$lib/hooks/use-actions";
|
import type { HTMLActionArray } from "$lib/hooks/use-actions";
|
||||||
import Render from "$lib/utils/Render.svelte";
|
import Render, { type TPassThroughProps } from "$lib/utils/Render.svelte";
|
||||||
const forwardEvents = forwardEventsBuilder(get_current_component());
|
|
||||||
|
/***** Props *****/
|
||||||
|
type TAsProp = $$Generic<SupportedAs>;
|
||||||
|
type $$Props = TSwitchGroupProps<typeof slotProps, TAsProp>;
|
||||||
|
|
||||||
export let as: SupportedAs = "div";
|
export let as: SupportedAs = "div";
|
||||||
export let use: HTMLActionArray = [];
|
export let use: HTMLActionArray = [];
|
||||||
|
|
||||||
|
/***** Events *****/
|
||||||
|
const forwardEvents = forwardEventsBuilder(get_current_component());
|
||||||
|
|
||||||
|
/***** Component *****/
|
||||||
let switchStore: StateDefinition["switchStore"] = writable(null);
|
let switchStore: StateDefinition["switchStore"] = writable(null);
|
||||||
|
|
||||||
let api = writable<StateDefinition>({
|
let api = writable<StateDefinition>({
|
||||||
@@ -36,18 +49,20 @@
|
|||||||
$switchStore.click();
|
$switchStore.click();
|
||||||
$switchStore.focus({ preventScroll: true });
|
$switchStore.focus({ preventScroll: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$: slotProps = {};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<Render
|
<Render
|
||||||
{...$$restProps}
|
{...$$restProps}
|
||||||
{as}
|
{as}
|
||||||
use={[...use, forwardEvents]}
|
use={[...use, forwardEvents]}
|
||||||
slotProps={{}}
|
{slotProps}
|
||||||
name={"SwitchGroup"}
|
name={"SwitchGroup"}
|
||||||
>
|
>
|
||||||
<DescriptionProvider name="SwitchDescription">
|
<DescriptionProvider name="SwitchDescription">
|
||||||
<LabelProvider name="SwitchLabel" {onClick}>
|
<LabelProvider name="SwitchLabel" {onClick}>
|
||||||
<slot />
|
<slot {...slotProps} />
|
||||||
</LabelProvider>
|
</LabelProvider>
|
||||||
</DescriptionProvider>
|
</DescriptionProvider>
|
||||||
</Render>
|
</Render>
|
||||||
|
|||||||
Reference in New Issue
Block a user