This commit is contained in:
+23
@@ -0,0 +1,23 @@
|
||||
import { type ReadableBoxedValues } from "svelte-toolbelt";
|
||||
import type { RefAttachment, WithRefOpts } from "../../internal/types.js";
|
||||
interface AspectRatioRootStateOpts extends WithRefOpts, ReadableBoxedValues<{
|
||||
ratio: number;
|
||||
}> {
|
||||
}
|
||||
export declare class AspectRatioRootState {
|
||||
static create(opts: AspectRatioRootStateOpts): AspectRatioRootState;
|
||||
readonly opts: AspectRatioRootStateOpts;
|
||||
readonly attachment: RefAttachment;
|
||||
constructor(opts: AspectRatioRootStateOpts);
|
||||
readonly props: {
|
||||
readonly id: string;
|
||||
readonly style: {
|
||||
readonly position: "absolute";
|
||||
readonly top: 0;
|
||||
readonly right: 0;
|
||||
readonly bottom: 0;
|
||||
readonly left: 0;
|
||||
};
|
||||
};
|
||||
}
|
||||
export {};
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
import { attachRef } from "svelte-toolbelt";
|
||||
import { createBitsAttrs } from "../../internal/attrs.js";
|
||||
const aspectRatioAttrs = createBitsAttrs({
|
||||
component: "aspect-ratio",
|
||||
parts: ["root"],
|
||||
});
|
||||
export class AspectRatioRootState {
|
||||
static create(opts) {
|
||||
return new AspectRatioRootState(opts);
|
||||
}
|
||||
opts;
|
||||
attachment;
|
||||
constructor(opts) {
|
||||
this.opts = opts;
|
||||
this.attachment = attachRef(this.opts.ref);
|
||||
}
|
||||
props = $derived.by(() => ({
|
||||
id: this.opts.id.current,
|
||||
style: {
|
||||
position: "absolute",
|
||||
top: 0,
|
||||
right: 0,
|
||||
bottom: 0,
|
||||
left: 0,
|
||||
},
|
||||
[aspectRatioAttrs.root]: "",
|
||||
...this.attachment,
|
||||
}));
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
<script lang="ts">
|
||||
import { boxWith, mergeProps } from "svelte-toolbelt";
|
||||
import type { AspectRatioRootProps } from "../types.js";
|
||||
import { AspectRatioRootState } from "../aspect-ratio.svelte.js";
|
||||
import { createId } from "../../../internal/create-id.js";
|
||||
|
||||
const uid = $props.id();
|
||||
|
||||
let {
|
||||
ref = $bindable(null),
|
||||
id = createId(uid),
|
||||
ratio = 1,
|
||||
children,
|
||||
child,
|
||||
...restProps
|
||||
}: AspectRatioRootProps = $props();
|
||||
|
||||
const rootState = AspectRatioRootState.create({
|
||||
id: boxWith(() => id),
|
||||
ref: boxWith(
|
||||
() => ref,
|
||||
(v) => (ref = v)
|
||||
),
|
||||
ratio: boxWith(() => ratio),
|
||||
});
|
||||
|
||||
const mergedProps = $derived(mergeProps(restProps, rootState.props));
|
||||
</script>
|
||||
|
||||
<div style:position="relative" style:width="100%" style:padding-bottom="{ratio ? 100 / ratio : 0}%">
|
||||
{#if child}
|
||||
{@render child({ props: mergedProps })}
|
||||
{:else}
|
||||
<div {...mergedProps}>
|
||||
{@render children?.()}
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
import type { AspectRatioRootProps } from "../types.js";
|
||||
declare const AspectRatio: import("svelte").Component<AspectRatioRootProps, {}, "ref">;
|
||||
type AspectRatio = ReturnType<typeof AspectRatio>;
|
||||
export default AspectRatio;
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
export { default as Root } from "./components/aspect-ratio.svelte";
|
||||
export type { AspectRatioRootProps as RootProps } from "./types.js";
|
||||
+1
@@ -0,0 +1 @@
|
||||
export { default as Root } from "./components/aspect-ratio.svelte";
|
||||
+1
@@ -0,0 +1 @@
|
||||
export * as AspectRatio from "./exports.js";
|
||||
+1
@@ -0,0 +1 @@
|
||||
export * as AspectRatio from "./exports.js";
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
import type { WithChild, Without } from "../../internal/types.js";
|
||||
import type { BitsPrimitiveDivAttributes } from "../../shared/attributes.js";
|
||||
export type AspectRatioRootPropsWithoutHTML = WithChild<{
|
||||
/**
|
||||
* The aspect ratio of the image.
|
||||
*
|
||||
* @defaultValue 1
|
||||
*/
|
||||
ratio?: number;
|
||||
}>;
|
||||
export type AspectRatioRootProps = AspectRatioRootPropsWithoutHTML & Without<BitsPrimitiveDivAttributes, AspectRatioRootPropsWithoutHTML>;
|
||||
+1
@@ -0,0 +1 @@
|
||||
export {};
|
||||
Reference in New Issue
Block a user