-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
76 additions
and
57 deletions.
There are no files selected for viewing
46 changes: 25 additions & 21 deletions
46
packages/bits-ui/src/lib/bits/label/components/label.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,35 @@ | ||
<script lang="ts"> | ||
import { createLabel, melt } from "@melt-ui/svelte"; | ||
import { getLabelData } from "../ctx.js"; | ||
import type { Events, Props } from "../index.js"; | ||
import { createDispatcher } from "$lib/internal/events.js"; | ||
import type { RootProps } from "../index.js"; | ||
import { setLabelRootState } from "../label.svelte.js"; | ||
import { readonlyBox } from "$lib/internal/box.svelte.js"; | ||
import { styleToString } from "$lib/internal/style.js"; | ||
type $$Props = Props; | ||
type $$Events = Events; | ||
let { | ||
onmousedown: onmousedownProp = () => {}, | ||
asChild, | ||
children, | ||
child, | ||
el = $bindable(), | ||
style, | ||
for: forProp, | ||
...restProps | ||
}: RootProps = $props(); | ||
export let asChild: $$Props["asChild"] = false; | ||
export let el: $$Props["el"] = undefined; | ||
const onmousedown = readonlyBox(() => onmousedownProp); | ||
const { | ||
elements: { root }, | ||
} = createLabel(); | ||
const dispatch = createDispatcher(); | ||
const { getAttrs } = getLabelData(); | ||
const attrs = getAttrs("root"); | ||
$: builder = $root; | ||
$: Object.assign(builder, attrs); | ||
const rootState = setLabelRootState({ onmousedown }); | ||
const mergedProps = $derived({ | ||
...restProps, | ||
...rootState.props, | ||
style: styleToString(style), | ||
for: forProp, | ||
} as const); | ||
</script> | ||
|
||
{#if asChild} | ||
<slot {builder} /> | ||
{@render child?.({ props: mergedProps })} | ||
{:else} | ||
<label bind:this={el} use:melt={builder} {...$$restProps} on:m-mousedown={dispatch}> | ||
<slot {builder} /> | ||
<label bind:this={el} {...mergedProps} for={forProp}> | ||
{@render children?.()} | ||
</label> | ||
{/if} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export { default as Root } from "./components/label.svelte"; | ||
|
||
export type { LabelProps as Props, LabelEvents as Events } from "./types.js"; | ||
export type { LabelRootProps as RootProps } from "./types.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { type ReadonlyBoxedValues, boxedState, readonlyBox } from "$lib/internal/box.svelte.js"; | ||
import { type EventCallback, composeHandlers } from "$lib/internal/events.js"; | ||
|
||
type LabelRootStateProps = ReadonlyBoxedValues<{ | ||
onmousedown: EventCallback<MouseEvent>; | ||
}>; | ||
|
||
class LabelRootState { | ||
#onmousedownProp = boxedState<LabelRootStateProps["onmousedown"]>(readonlyBox(() => () => {})); | ||
|
||
constructor(props: LabelRootStateProps) { | ||
this.#onmousedownProp.value = props.onmousedown; | ||
} | ||
|
||
#onmousedown = composeHandlers(this.#onmousedownProp, (e) => { | ||
if (e.detail > 1) e.preventDefault(); | ||
}); | ||
|
||
get props() { | ||
return { | ||
onmousedown: this.#onmousedown, | ||
}; | ||
} | ||
} | ||
|
||
export function setLabelRootState(props: LabelRootStateProps) { | ||
return new LabelRootState(props); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import type { HTMLLabelAttributes } from "svelte/elements"; | ||
import type { CustomEventHandler } from "$lib/index.js"; | ||
import type { DOMElement } from "$lib/internal/types.js"; | ||
import type { PrimitiveLabelAttributes, WithAsChild } from "$lib/internal/types.js"; | ||
import type { EventCallback } from "$lib/internal/events.js"; | ||
|
||
export type LabelPropsWithoutHTML = DOMElement<HTMLLabelElement>; | ||
|
||
export type LabelProps = LabelPropsWithoutHTML & HTMLLabelAttributes; | ||
|
||
export type LabelEvents<T extends Element = HTMLLabelElement> = { | ||
mousedown: CustomEventHandler<MouseEvent, T>; | ||
export type LabelRootPropsWithoutHTML = WithAsChild<{ | ||
for: string; | ||
}> & { | ||
onmousedown?: EventCallback<MouseEvent>; | ||
}; | ||
|
||
export type LabelRootProps = LabelRootPropsWithoutHTML & | ||
Omit<PrimitiveLabelAttributes, "onmousedown" | "for">; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters