Lib: Add imgwidth/imgheight to Card component so layout doesn't jump when lazyloading images
This commit is contained in:
@ -1,5 +1,6 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import type { Snippet } from "svelte";
|
import type { Snippet } from "svelte";
|
||||||
|
import LazyImage from "./LazyImage.svelte";
|
||||||
|
|
||||||
interface CardProps {
|
interface CardProps {
|
||||||
children: Snippet;
|
children: Snippet;
|
||||||
@ -10,6 +11,12 @@
|
|||||||
/** The id of the header image element for JS access. */
|
/** The id of the header image element for JS access. */
|
||||||
imgid?: string | undefined;
|
imgid?: string | undefined;
|
||||||
|
|
||||||
|
/** The aspect ratio width used to reserve image space (while its loading) */
|
||||||
|
imgwidth?: number;
|
||||||
|
|
||||||
|
/** The aspect ratio height used to reserve image space (while its loading) */
|
||||||
|
imgheight?: number;
|
||||||
|
|
||||||
/** Hide the header image element. It can be shown by removing the "hidden" property using JS and the imgid. */
|
/** Hide the header image element. It can be shown by removing the "hidden" property using JS and the imgid. */
|
||||||
imghidden?: boolean;
|
imghidden?: boolean;
|
||||||
|
|
||||||
@ -21,6 +28,8 @@
|
|||||||
children,
|
children,
|
||||||
imgsrc = undefined,
|
imgsrc = undefined,
|
||||||
imgid = undefined,
|
imgid = undefined,
|
||||||
|
imgwidth = undefined,
|
||||||
|
imgheight = undefined,
|
||||||
imghidden = false,
|
imghidden = false,
|
||||||
fullwidth = false,
|
fullwidth = false,
|
||||||
...restProps
|
...restProps
|
||||||
@ -30,14 +39,16 @@
|
|||||||
<div class="card overflow-hidden bg-white shadow {fullwidth ? 'w-full' : 'w-auto'}">
|
<div class="card overflow-hidden bg-white shadow {fullwidth ? 'w-full' : 'w-auto'}">
|
||||||
<!-- Allow empty strings for images that only appear after user action -->
|
<!-- Allow empty strings for images that only appear after user action -->
|
||||||
{#if imgsrc !== undefined}
|
{#if imgsrc !== undefined}
|
||||||
<img
|
<div style="width: auto; aspect-ratio: {imgwidth} / {imgheight};">
|
||||||
id={imgid}
|
<LazyImage
|
||||||
src={imgsrc}
|
id={imgid}
|
||||||
alt="Card header"
|
src={imgsrc}
|
||||||
draggable="false"
|
alt="Card header"
|
||||||
class="select-none shadow"
|
draggable="false"
|
||||||
hidden={imghidden}
|
class="select-none shadow"
|
||||||
/>
|
hidden={imghidden}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
<div class="p-2" {...restProps}>
|
<div class="p-2" {...restProps}>
|
||||||
{@render children()}
|
{@render children()}
|
||||||
|
|||||||
@ -6,6 +6,7 @@
|
|||||||
import type { Driver } from "$lib/schema";
|
import type { Driver } from "$lib/schema";
|
||||||
import Input from "./Input.svelte";
|
import Input from "./Input.svelte";
|
||||||
import Dropdown, { type DropdownOption } from "./Dropdown.svelte";
|
import Dropdown, { type DropdownOption } from "./Dropdown.svelte";
|
||||||
|
import { DRIVER_HEADSHOT_HEIGHT, DRIVER_HEADSHOT_WIDTH } from "$lib/config";
|
||||||
|
|
||||||
interface DriverCardProps {
|
interface DriverCardProps {
|
||||||
/** The [Driver] object used to prefill values. */
|
/** The [Driver] object used to prefill values. */
|
||||||
@ -34,7 +35,7 @@
|
|||||||
driver = undefined,
|
driver = undefined,
|
||||||
disable_inputs = false,
|
disable_inputs = false,
|
||||||
require_inputs = false,
|
require_inputs = false,
|
||||||
headshot_template = "",
|
headshot_template = undefined,
|
||||||
team_select_value,
|
team_select_value,
|
||||||
team_select_options,
|
team_select_options,
|
||||||
active_value,
|
active_value,
|
||||||
@ -43,6 +44,8 @@
|
|||||||
|
|
||||||
<Card
|
<Card
|
||||||
imgsrc={driver?.headshot_url ?? headshot_template}
|
imgsrc={driver?.headshot_url ?? headshot_template}
|
||||||
|
imgwidth={DRIVER_HEADSHOT_WIDTH}
|
||||||
|
imgheight={DRIVER_HEADSHOT_HEIGHT}
|
||||||
imgid="update_driver_headshot_preview_{driver?.id ?? 'create'}"
|
imgid="update_driver_headshot_preview_{driver?.id ?? 'create'}"
|
||||||
>
|
>
|
||||||
<form method="POST" enctype="multipart/form-data">
|
<form method="POST" enctype="multipart/form-data">
|
||||||
|
|||||||
@ -6,6 +6,7 @@
|
|||||||
import type { Race } from "$lib/schema";
|
import type { Race } from "$lib/schema";
|
||||||
import Input from "./Input.svelte";
|
import Input from "./Input.svelte";
|
||||||
import { format } from "date-fns";
|
import { format } from "date-fns";
|
||||||
|
import { RACE_PICTOGRAM_HEIGHT, RACE_PICTOGRAM_WIDTH } from "$lib/config";
|
||||||
|
|
||||||
interface RaceCardProps {
|
interface RaceCardProps {
|
||||||
/** The [Race] object used to prefill values. */
|
/** The [Race] object used to prefill values. */
|
||||||
@ -56,6 +57,8 @@
|
|||||||
|
|
||||||
<Card
|
<Card
|
||||||
imgsrc={race?.pictogram_url ?? pictogram_template}
|
imgsrc={race?.pictogram_url ?? pictogram_template}
|
||||||
|
imgwidth={RACE_PICTOGRAM_WIDTH}
|
||||||
|
imgheight={RACE_PICTOGRAM_HEIGHT}
|
||||||
imgid="update_race_pictogram_preview_{race?.id ?? 'create'}"
|
imgid="update_race_pictogram_preview_{race?.id ?? 'create'}"
|
||||||
>
|
>
|
||||||
<form method="POST" enctype="multipart/form-data">
|
<form method="POST" enctype="multipart/form-data">
|
||||||
|
|||||||
@ -5,6 +5,7 @@
|
|||||||
import { get_by_value } from "$lib/database";
|
import { get_by_value } from "$lib/database";
|
||||||
import Dropdown, { type DropdownOption } from "./Dropdown.svelte";
|
import Dropdown, { type DropdownOption } from "./Dropdown.svelte";
|
||||||
import type { Action } from "svelte/action";
|
import type { Action } from "svelte/action";
|
||||||
|
import { DRIVER_HEADSHOT_HEIGHT, DRIVER_HEADSHOT_WIDTH } from "$lib/config";
|
||||||
|
|
||||||
interface SubstitutionCardProps {
|
interface SubstitutionCardProps {
|
||||||
/** The [Substitution] object used to prefill values. */
|
/** The [Substitution] object used to prefill values. */
|
||||||
@ -70,7 +71,7 @@
|
|||||||
`update_substitution_headshot_preview_${substitution?.id ?? "create"}`,
|
`update_substitution_headshot_preview_${substitution?.id ?? "create"}`,
|
||||||
) as HTMLImageElement;
|
) as HTMLImageElement;
|
||||||
|
|
||||||
preview.src = src;
|
if (preview) preview.src = src;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
@ -78,6 +79,8 @@
|
|||||||
<Card
|
<Card
|
||||||
imgsrc={get_by_value(drivers, "id", substitution?.substitute ?? "")?.headshot_url ??
|
imgsrc={get_by_value(drivers, "id", substitution?.substitute ?? "")?.headshot_url ??
|
||||||
headshot_template}
|
headshot_template}
|
||||||
|
imgwidth={DRIVER_HEADSHOT_WIDTH}
|
||||||
|
imgheight={DRIVER_HEADSHOT_HEIGHT}
|
||||||
imgid="update_substitution_headshot_preview_{substitution?.id ?? 'create'}"
|
imgid="update_substitution_headshot_preview_{substitution?.id ?? 'create'}"
|
||||||
>
|
>
|
||||||
<form method="POST" enctype="multipart/form-data">
|
<form method="POST" enctype="multipart/form-data">
|
||||||
|
|||||||
@ -5,6 +5,7 @@
|
|||||||
import Button from "./Button.svelte";
|
import Button from "./Button.svelte";
|
||||||
import type { Team } from "$lib/schema";
|
import type { Team } from "$lib/schema";
|
||||||
import Input from "./Input.svelte";
|
import Input from "./Input.svelte";
|
||||||
|
import { TEAM_LOGO_HEIGHT, TEAM_LOGO_WIDTH } from "$lib/config";
|
||||||
|
|
||||||
interface TeamCardProps {
|
interface TeamCardProps {
|
||||||
/** The [Team] object used to prefill values. */
|
/** The [Team] object used to prefill values. */
|
||||||
@ -30,6 +31,8 @@
|
|||||||
|
|
||||||
<Card
|
<Card
|
||||||
imgsrc={team?.logo_url ?? logo_template}
|
imgsrc={team?.logo_url ?? logo_template}
|
||||||
|
imgwidth={TEAM_LOGO_WIDTH}
|
||||||
|
imgheight={TEAM_LOGO_HEIGHT}
|
||||||
imgid="update_team_logo_preview_{team?.id ?? 'create'}"
|
imgid="update_team_logo_preview_{team?.id ?? 'create'}"
|
||||||
>
|
>
|
||||||
<form method="POST" enctype="multipart/form-data">
|
<form method="POST" enctype="multipart/form-data">
|
||||||
|
|||||||
Reference in New Issue
Block a user