tweaks control button positioning css

This commit is contained in:
MinamiFunakoshiTR 2025-03-26 17:28:27 -07:00
parent e261317e54
commit 371b81e1f5
Failed to extract signature
3 changed files with 25 additions and 35 deletions

View file

@ -106,10 +106,10 @@
let resetCondition = $derived(time >= duration); // - 0.1; let resetCondition = $derived(time >= duration); // - 0.1;
// Dimensions etc other useful things // Dimensions etc other useful things
let heightVideo = $state(0); let videoHeight = $state(0);
let widthVideo = $state(0); let videoWidth = $state(0);
let heightVideoContainer = $state(0); let videoHeightContainer = $state(0);
let widthVideoContainer = $state(0); let videoWidthContainer = $state(0);
// For intersection observer // For intersection observer
let intersecting = $state(false); let intersecting = $state(false);
@ -123,7 +123,7 @@
let interactiveControlsOpacity = $state(controlsOpacityMax); let interactiveControlsOpacity = $state(controlsOpacityMax);
// Get control button positioning // Get control button positioning
let controlButtonPosition = $derived.by(() => let controlButtonPosition = $derived(
getButtonPosition(controlsPosition, controlsBorderOffset) getButtonPosition(controlsPosition, controlsBorderOffset)
); );
@ -168,10 +168,7 @@
}} }}
style=" style="
opacity: {interactiveControlsOpacity}; opacity: {interactiveControlsOpacity};
top: {controlButtonPosition.top}px; {controlButtonPosition}
bottom: {controlButtonPosition.bottom}px;
left: {controlButtonPosition.left}px;
right: {controlButtonPosition.right}px;
" "
> >
{#if resetCondition} {#if resetCondition}
@ -204,7 +201,7 @@
onclick={() => { onclick={() => {
paused = !paused; paused = !paused;
}} }}
style="top: 0; left: 0; width: {widthVideoContainer}px; height: {heightVideoContainer}px;" style="top: 0; left: 0; width: {videoWidthContainer}px; height: {videoHeightContainer}px;"
></button> ></button>
{/snippet} {/snippet}
@ -253,8 +250,8 @@
bind:this={element} bind:this={element}
class="video-wrapper relative block" class="video-wrapper relative block"
aria-hidden={hidden} aria-hidden={hidden}
bind:clientWidth={widthVideoContainer} bind:clientWidth={videoWidthContainer}
bind:clientHeight={heightVideoContainer} bind:clientHeight={videoHeightContainer}
> >
{#if possibleToPlayPause} {#if possibleToPlayPause}
{#if showControls} {#if showControls}
@ -276,8 +273,8 @@
bind:currentTime={time} bind:currentTime={time}
bind:duration bind:duration
bind:paused bind:paused
bind:clientWidth={widthVideo} bind:clientWidth={videoWidth}
bind:clientHeight={heightVideo} bind:clientHeight={videoHeight}
> >
<track kind="captions" /> <track kind="captions" />
</video> </video>
@ -288,8 +285,8 @@
<div <div
class="video-wrapper relative" class="video-wrapper relative"
aria-hidden={hidden} aria-hidden={hidden}
bind:clientWidth={widthVideoContainer} bind:clientWidth={videoWidthContainer}
bind:clientHeight={heightVideoContainer} bind:clientHeight={videoHeightContainer}
> >
{#if possibleToPlayPause} {#if possibleToPlayPause}
{#if showControls} {#if showControls}
@ -312,8 +309,8 @@
bind:duration bind:duration
bind:paused bind:paused
autoplay autoplay
bind:clientWidth={widthVideo} bind:clientWidth={videoWidth}
bind:clientHeight={heightVideo} bind:clientHeight={videoHeight}
> >
<track kind="captions" /> <track kind="captions" />
</video> </video>

View file

@ -1,4 +1,5 @@
export type ControlsPosition = export type ControlsPosition =
| 'middle'
| 'top right' | 'top right'
| 'top left' | 'top left'
| 'bottom right' | 'bottom right'

View file

@ -1,27 +1,19 @@
import type { ControlsPosition } from './types'; import type { ControlsPosition } from './types';
/** Returns the pixel values for positioning the controls button */ /** Returns the CSS positions for the controls button */
export const getButtonPosition = ( export const getButtonPosition = (
controlsPosition: ControlsPosition, controlsPosition: ControlsPosition,
borderOffset: number borderOffset: number
) => { ) => {
if (controlsPosition === 'top left') if (controlsPosition === 'top left')
return { return `top: ${borderOffset}px; left: ${borderOffset}px;`;
top: borderOffset, if (controlsPosition === 'top right')
left: borderOffset, return `top: ${borderOffset}px; right: ${borderOffset}px;`;
};
if (controlsPosition === 'bottom left') if (controlsPosition === 'bottom left')
return { return `bottom: ${borderOffset}px; left: ${borderOffset}px;`;
bottom: borderOffset,
left: borderOffset,
};
if (controlsPosition === 'bottom right') if (controlsPosition === 'bottom right')
return { return `bottom: ${borderOffset}px; right: ${borderOffset}px;`;
bottom: borderOffset,
right: borderOffset, // Otherwise, centre it
}; return `top: 50%; left: 50%; transform: translate(-50%, -50%);`;
return {
top: borderOffset,
right: borderOffset,
};
}; };