-
-
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
8 changed files
with
112 additions
and
134 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
packages/bits-ui/src/lib/bits/aspect-ratio/aspect-ratio.svelte.ts
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,49 @@ | ||
import { useRefById, type ReadableBoxedValues, type WithRefProps } from "$lib/internal/index.js"; | ||
|
||
const ASPECT_RATIO_ROOT_ATTR = "data-aspect-ratio-root"; | ||
|
||
type AspectRatioRootStateProps = WithRefProps<ReadableBoxedValues<{ ratio: number }>>; | ||
|
||
export class AspectRatioRootState { | ||
#ref: AspectRatioRootStateProps["ref"]; | ||
#id: AspectRatioRootStateProps["id"]; | ||
#ratio: AspectRatioRootStateProps["ratio"]; | ||
|
||
constructor(props: AspectRatioRootStateProps) { | ||
this.#ref = props.ref; | ||
this.#id = props.id; | ||
this.#ratio = props.ratio; | ||
|
||
useRefById({ | ||
id: this.#id, | ||
ref: this.#ref, | ||
}); | ||
} | ||
|
||
wrapperProps = $derived.by(() => ({ | ||
style: { | ||
position: "relative", | ||
width: "100%", | ||
paddingBottom: `${this.#ratio.current ? 100 / this.#ratio.current : 0}%}`, | ||
}, | ||
})); | ||
|
||
props = $derived.by( | ||
() => | ||
({ | ||
id: this.#id.current, | ||
style: { | ||
position: "absolute", | ||
top: 0, | ||
right: 0, | ||
bottom: 0, | ||
left: 0, | ||
}, | ||
[ASPECT_RATIO_ROOT_ATTR]: "", | ||
}) as const | ||
); | ||
} | ||
|
||
export function useAspectRatioRoot(props: AspectRatioRootStateProps) { | ||
return new AspectRatioRootState(props); | ||
} |
43 changes: 30 additions & 13 deletions
43
packages/bits-ui/src/lib/bits/aspect-ratio/components/aspect-ratio.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,20 +1,37 @@ | ||
<script lang="ts"> | ||
import { box } from "svelte-toolbelt"; | ||
import type { RootProps } from "../index.js"; | ||
import { useAspectRatioRoot } from "../aspect-ratio.svelte.js"; | ||
import { mergeProps } from "$lib/internal/mergeProps.js"; | ||
import { useId } from "$lib/internal/useId.js"; | ||
let { ratio, children, ref = $bindable(), ...restProps }: RootProps = $props(); | ||
let { | ||
ref = $bindable(null), | ||
id = useId(), | ||
ratio = 1, | ||
children, | ||
child, | ||
...restProps | ||
}: RootProps = $props(); | ||
const rootState = useAspectRatioRoot({ | ||
id: box.with(() => id), | ||
ref: box.with( | ||
() => ref, | ||
(v) => (ref = v) | ||
), | ||
ratio: box.with(() => ratio), | ||
}); | ||
const mergedProps = $derived(mergeProps(restProps, rootState.props)); | ||
</script> | ||
|
||
<div style:position="relative" style:width="100%" style:padding-bottom="{ratio ? 100 / ratio : 0}%"> | ||
<div | ||
bind:this={ref} | ||
style:position="absolute" | ||
style:top="0" | ||
style:right="0" | ||
style:bottom="0" | ||
style:left="0" | ||
{...restProps} | ||
data-aspect-ratio-root="" | ||
> | ||
{@render children?.()} | ||
</div> | ||
{#if child} | ||
{@render child({ props: mergedProps })} | ||
{:else} | ||
<div {...mergedProps}> | ||
{@render children?.()} | ||
</div> | ||
{/if} | ||
</div> |
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,9 +1,8 @@ | ||
import type { HTMLDivAttributes } from "$lib/internal/index.js"; | ||
import type { Expand } from "$lib/internal/index.js"; | ||
import type { PrimitiveDivAttributes, WithChild, Without } from "$lib/internal/index.js"; | ||
|
||
export type AspectRatioPropsWithoutHTML = Expand<{ | ||
export type AspectRatioPropsWithoutHTML = WithChild<{ | ||
ratio?: number; | ||
ref?: HTMLElement | null; | ||
}>; | ||
|
||
export type AspectRatioProps = AspectRatioPropsWithoutHTML & HTMLDivAttributes; | ||
export type AspectRatioProps = AspectRatioPropsWithoutHTML & | ||
Without<PrimitiveDivAttributes, AspectRatioPropsWithoutHTML>; |
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
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