-
-
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
29 changed files
with
649 additions
and
503 deletions.
There are no files selected for viewing
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
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
26 changes: 0 additions & 26 deletions
26
packages/bits-ui/src/lib/bits/link-preview/components/link-preview-arrow.svelte
This file was deleted.
Oops, something went wrong.
203 changes: 73 additions & 130 deletions
203
packages/bits-ui/src/lib/bits/link-preview/components/link-preview-content.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,136 +1,79 @@ | ||
<script lang="ts"> | ||
import { melt } from "@melt-ui/svelte"; | ||
import { getCtx, updatePositioning } from "../ctx.js"; | ||
import type { ContentEvents, ContentProps } from "../index.js"; | ||
import { type Transition, createDispatcher } from "$lib/internal/index.js"; | ||
import { box } from "svelte-toolbelt"; | ||
import type { ContentProps } from "../index.js"; | ||
import { useLinkPreviewContent } from "../link-preview.svelte.js"; | ||
import { useId } from "$lib/internal/useId.svelte.js"; | ||
import { mergeProps } from "$lib/internal/mergeProps.js"; | ||
import PopperLayer from "$lib/bits/utilities/popper-layer/popper-layer.svelte"; | ||
type T = $$Generic<Transition>; | ||
type In = $$Generic<Transition>; | ||
type Out = $$Generic<Transition>; | ||
let { | ||
children, | ||
child, | ||
id = useId(), | ||
ref = $bindable(null), | ||
side = "top", | ||
sideOffset = 0, | ||
align = "center", | ||
avoidCollisions = true, | ||
arrowPadding = 0, | ||
sticky = "partial", | ||
hideWhenDetached = false, | ||
collisionPadding = 0, | ||
onInteractOutside, | ||
onEscapeKeydown, | ||
forceMount = false, | ||
...restProps | ||
}: ContentProps = $props(); | ||
type $$Props = ContentProps<T, In, Out>; | ||
type $$Events = ContentEvents; | ||
const contentState = useLinkPreviewContent({ | ||
id: box.with(() => id), | ||
ref: box.with( | ||
() => ref, | ||
(v) => (ref = v) | ||
), | ||
}); | ||
export let transition: $$Props["transition"] = undefined; | ||
export let transitionConfig: $$Props["transitionConfig"] = undefined; | ||
export let inTransition: $$Props["inTransition"] = undefined; | ||
export let inTransitionConfig: $$Props["inTransitionConfig"] = undefined; | ||
export let outTransition: $$Props["outTransition"] = undefined; | ||
export let outTransitionConfig: $$Props["outTransitionConfig"] = undefined; | ||
export let asChild: $$Props["asChild"] = false; | ||
export let id: $$Props["id"] = undefined; | ||
export let side: $$Props["side"] = "bottom"; | ||
export let align: $$Props["align"] = "center"; | ||
export let sideOffset: $$Props["sideOffset"] = 0; | ||
export let alignOffset: $$Props["alignOffset"] = 0; | ||
export let collisionPadding: $$Props["collisionPadding"] = 8; | ||
export let avoidCollisions: $$Props["avoidCollisions"] = true; | ||
export let collisionBoundary: $$Props["collisionBoundary"] = undefined; | ||
export let sameWidth: $$Props["sameWidth"] = false; | ||
export let fitViewport: $$Props["fitViewport"] = false; | ||
export let strategy: $$Props["strategy"] = "absolute"; | ||
export let overlap: $$Props["overlap"] = false; | ||
export let el: $$Props["el"] = undefined; | ||
const { | ||
elements: { content }, | ||
states: { open }, | ||
ids, | ||
getAttrs, | ||
} = getCtx(); | ||
$: if (id) { | ||
ids.content.set(id); | ||
} | ||
const attrs = getAttrs("content"); | ||
const dispatch = createDispatcher(); | ||
$: builder = $content; | ||
$: Object.assign(builder, attrs); | ||
$: if ($open) { | ||
updatePositioning({ | ||
side, | ||
align, | ||
sideOffset, | ||
alignOffset, | ||
collisionPadding, | ||
avoidCollisions, | ||
collisionBoundary, | ||
sameWidth, | ||
fitViewport, | ||
strategy, | ||
overlap, | ||
}); | ||
} | ||
const floatingProps = $derived({ | ||
side, | ||
sideOffset, | ||
align, | ||
avoidCollisions, | ||
arrowPadding, | ||
sticky, | ||
hideWhenDetached, | ||
collisionPadding, | ||
}); | ||
</script> | ||
|
||
{#if asChild && $open} | ||
<slot {builder} /> | ||
{:else if transition && $open} | ||
<div | ||
bind:this={ref} | ||
transition:transition={transitionConfig} | ||
use:melt={builder} | ||
{...$$restProps} | ||
on:m-focusout={dispatch} | ||
on:m-pointerdown={dispatch} | ||
on:m-pointerenter={dispatch} | ||
on:m-pointerleave={dispatch} | ||
> | ||
<slot {builder} /> | ||
</div> | ||
{:else if inTransition && outTransition && $open} | ||
<div | ||
bind:this={ref} | ||
in:inTransition={inTransitionConfig} | ||
out:outTransition={outTransitionConfig} | ||
use:melt={builder} | ||
{...$$restProps} | ||
on:m-focusout={dispatch} | ||
on:m-pointerdown={dispatch} | ||
on:m-pointerenter={dispatch} | ||
on:m-pointerleave={dispatch} | ||
> | ||
<slot {builder} /> | ||
</div> | ||
{:else if inTransition && $open} | ||
<div | ||
bind:this={ref} | ||
in:inTransition={inTransitionConfig} | ||
use:melt={builder} | ||
{...$$restProps} | ||
on:m-focusout={dispatch} | ||
on:m-pointerdown={dispatch} | ||
on:m-pointerenter={dispatch} | ||
on:m-pointerleave={dispatch} | ||
> | ||
<slot {builder} /> | ||
</div> | ||
{:else if outTransition && $open} | ||
<div | ||
bind:this={ref} | ||
out:outTransition={outTransitionConfig} | ||
use:melt={builder} | ||
{...$$restProps} | ||
on:m-focusout={dispatch} | ||
on:m-pointerdown={dispatch} | ||
on:m-pointerenter={dispatch} | ||
on:m-pointerleave={dispatch} | ||
> | ||
<slot {builder} /> | ||
</div> | ||
{:else if $open} | ||
<div | ||
bind:this={ref} | ||
use:melt={builder} | ||
{...$$restProps} | ||
on:m-focusout={dispatch} | ||
on:m-pointerdown={dispatch} | ||
on:m-pointerenter={dispatch} | ||
on:m-pointerleave={dispatch} | ||
> | ||
<slot {builder} /> | ||
</div> | ||
{/if} | ||
<PopperLayer | ||
{...restProps} | ||
{...floatingProps} | ||
present={contentState.root.open.value || forceMount} | ||
{id} | ||
onInteractOutside={(e) => { | ||
onInteractOutside?.(e); | ||
if (e.defaultPrevented) return; | ||
contentState.root.immediateClose(); | ||
}} | ||
onEscapeKeydown={(e) => { | ||
// TODO: users should be able to cancel this | ||
onEscapeKeydown?.(e); | ||
contentState.root.immediateClose(); | ||
}} | ||
onMountAutoFocus={(e) => e.preventDefault()} | ||
onDestroyAutoFocus={(e) => e.preventDefault()} | ||
trapped={false} | ||
loop={false} | ||
preventScroll={false} | ||
> | ||
{#snippet popper({ props })} | ||
{@const mergedProps = mergeProps(restProps, contentState.props, props)} | ||
{#if child} | ||
{@render child?.({ props: mergedProps })} | ||
{:else} | ||
<div {...mergedProps} bind:this={ref}> | ||
{@render children?.()} | ||
</div> | ||
{/if} | ||
{/snippet} | ||
</PopperLayer> |
70 changes: 30 additions & 40 deletions
70
packages/bits-ui/src/lib/bits/link-preview/components/link-preview-trigger.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,46 +1,36 @@ | ||
<script lang="ts"> | ||
import { melt } from "@melt-ui/svelte"; | ||
import { getCtx } from "../ctx.js"; | ||
import type { TriggerEvents, TriggerProps } from "../index.js"; | ||
import { createDispatcher } from "$lib/internal/events.js"; | ||
import { box } from "svelte-toolbelt"; | ||
import type { TriggerProps } from "../index.js"; | ||
import { useLinkPreviewTrigger } from "../link-preview.svelte.js"; | ||
import { useId } from "$lib/internal/useId.svelte.js"; | ||
import { mergeProps } from "$lib/internal/mergeProps.js"; | ||
import { FloatingLayer } from "$lib/bits/utilities/floating-layer/index.js"; | ||
type $$Props = TriggerProps; | ||
type $$Events = TriggerEvents; | ||
let { | ||
ref = $bindable(null), | ||
id = useId(), | ||
child, | ||
children, | ||
...restProps | ||
}: TriggerProps = $props(); | ||
export let asChild: $$Props["asChild"] = false; | ||
export let id: $$Props["id"] = undefined; | ||
export let el: $$Props["el"] = undefined; | ||
const triggerState = useLinkPreviewTrigger({ | ||
id: box.with(() => id), | ||
ref: box.with( | ||
() => ref, | ||
(v) => (ref = v) | ||
), | ||
}); | ||
const { | ||
elements: { trigger }, | ||
ids, | ||
getAttrs, | ||
} = getCtx(); | ||
const dispatch = createDispatcher(); | ||
const attrs = getAttrs("trigger"); | ||
$: if (id) { | ||
ids.trigger.set(id); | ||
} | ||
$: builder = $trigger; | ||
$: Object.assign(builder, attrs); | ||
const mergedProps = $derived(mergeProps(restProps, triggerState.props)); | ||
</script> | ||
|
||
{#if asChild} | ||
<slot {builder} /> | ||
{:else} | ||
<svelte:element | ||
this="a" | ||
bind:this={ref} | ||
use:melt={builder} | ||
{...$$restProps} | ||
{...attrs} | ||
on:m-blur={dispatch} | ||
on:m-focus={dispatch} | ||
on:m-pointerenter={dispatch} | ||
on:m-pointerleave={dispatch} | ||
> | ||
<slot {builder} /> | ||
</svelte:element> | ||
{/if} | ||
<FloatingLayer.Anchor {id}> | ||
{#if child} | ||
{@render child?.({ props: mergedProps })} | ||
{:else} | ||
<a {...mergedProps}> | ||
{@render children?.()} | ||
</a> | ||
{/if} | ||
</FloatingLayer.Anchor> |
Oops, something went wrong.