Skip to content

Commit

Permalink
feat: add screenBreakpoint store and make isMobile a derived
Browse files Browse the repository at this point in the history
  • Loading branch information
VmMad committed May 13, 2024
1 parent f399ec7 commit 153fb93
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 39 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@
},
"./enums": "./dist/enums/index.js",
"./preset": "./dist/tailwind/tailwind.preset.js",
"./constants": "./dist/constants/index.js"
"./constants": "./dist/constants/index.js",
"./stores": "./dist/stores/index.js"
},
"types": "./dist/components/index.d.ts",
"lint-staged": {
Expand Down
6 changes: 3 additions & 3 deletions src/lib/components/atoms/animation/animation.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import Lottie, { type AnimationDirection, type AnimationItem } from 'lottie-web'
import { onMount } from 'svelte'
import { RendererType } from './animation.enums'
import { isMobile } from '$lib/stores'
import { isSmallScreen } from '$lib/stores'
export let src: string =
'https://lottie.host/05761115-2753-4236-8e02-049e9b61969f/X6gyzVzo8S.json'
Expand Down Expand Up @@ -63,7 +63,7 @@
renderer,
container: player,
loop,
autoplay: $isMobile || (!playOnHover && autoplay),
autoplay: $isSmallScreen || (!playOnHover && autoplay),
path: src,
})
})
Expand All @@ -74,7 +74,7 @@
class:pointer-events-none={pointerEventsNone}
style:--background-color={backgroundColor}
{src}
autoplay={isMobile || (!playOnHover && autoplay)}
autoplay={isSmallScreen || (!playOnHover && autoplay)}
{loop}
role="img"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { IconEnum, Icon } from '$components'
import { OVERLINE_TEXT } from '$components/atoms/title/title.classes'
import { Position, Align } from '$lib/enums'
import { isMobile } from '$lib/stores'
import { isSmallScreen } from '$lib/stores'
import { MediaManager, type Media } from '../media-manager'
import { HighlightCardVariant } from './highlight-card.enums'
import {
Expand Down Expand Up @@ -96,12 +96,12 @@
$: isHoverVariant = variant === HighlightCardVariant.Hover
function handleMouseEnter(): void {
if (!$isMobile) {
if (!$isSmallScreen) {
isHovered = true
}
}
function handleMouseLeave(): void {
if (!$isMobile) {
if (!$isSmallScreen) {
isHovered = false
}
}
Expand All @@ -113,7 +113,7 @@
on:mouseenter={handleMouseEnter}
on:mouseleave={handleMouseLeave}
class="highlight-card flex flex-col {backgroundColor} {itemsAlignClass} {BORDER_RADIUS}"
class:variant--hover={isHoverVariant}
class:variant--hover={isHoverVariant && !$isSmallScreen}
{...link ? { ...externalLinkProps, href: link, role: 'link', tabindex: 0 } : {}}
>
{#if backgroundMedia}
Expand All @@ -124,7 +124,7 @@
media={backgroundMedia}
pointerEventsNone
{isHovered}
playOnHover={isHoverVariant && !$isMobile}
playOnHover={isHoverVariant}
/>
</div>
{/if}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { MediaType } from '..'
import { IMAGE_DEFAULT_PROPS, VIDEO_DEFAULT_PROPS } from './media-manager.constants'
import { Animation } from '$atoms'
import { isMobile } from '$lib/stores'
import { isSmallScreen } from '$lib/stores'
export let media: Media
export let pointerEventsNone: boolean = false
Expand Down Expand Up @@ -34,7 +34,7 @@
}
}
$: if ($isMobile && videoElement && videoMedia?.autoplay) {
$: if ($isSmallScreen && videoElement && videoMedia?.autoplay) {
void videoElement.play()
}
</script>
Expand All @@ -53,7 +53,7 @@
} = videoMedia}
<video
bind:this={videoElement}
autoplay={$isMobile || (!playOnHover && autoplay)}
autoplay={$isSmallScreen || (!playOnHover && autoplay)}
{loop}
muted
{poster}
Expand Down
11 changes: 6 additions & 5 deletions src/lib/constants/tailwind.constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { ScreenSize } from '../enums/screen-size.enum'
import colors from 'tailwindcss/colors.js'

export const CUSTOM_COLORS: Record<string, Record<number, string>> = {
Expand Down Expand Up @@ -62,11 +63,11 @@ export const FONT_FAMILY_CLASS: IFontFamily = {
secondary: 'font-inter',
}

export const BREAKPOINTS: Record<string, number> = {
sm: 768,
md: 1024,
lg: 1400,
xl: 1920,
export const BREAKPOINTS: Record<ScreenSize, number> = {
[ScreenSize.Sm]: 768,
[ScreenSize.Md]: 1024,
[ScreenSize.Lg]: 1400,
[ScreenSize.Xl]: 1920,
}

const {
Expand Down
1 change: 1 addition & 0 deletions src/lib/enums/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './mode.enum'
export * from './direction.enum'
export * from './position.enum'
export * from './align.enum'
export * from './screen-size.enum'
6 changes: 6 additions & 0 deletions src/lib/enums/screen-size.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum ScreenSize {
Sm = 'sm',
Md = 'md',
Lg = 'lg',
Xl = 'xl',
}
2 changes: 1 addition & 1 deletion src/lib/stores/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './isMobile.store'
export * from './screen-breakpoint.store'
21 changes: 0 additions & 21 deletions src/lib/stores/isMobile.store.ts

This file was deleted.

43 changes: 43 additions & 0 deletions src/lib/stores/screen-breakpoint.store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { BREAKPOINTS } from '$lib/constants'
import { ScreenSize } from '$lib/enums'
import { derived, writable, type Readable, type Writable } from 'svelte/store'

// Create a writable store with an initial value based on the current screen width
export const screenBreakpoint: Writable<ScreenSize> = writable(getCurrentScreenBreakpoint())
export const isMobile: Readable<boolean> = derived(
screenBreakpoint,
($screenBreakPoint) => $screenBreakPoint === ScreenSize.Sm
)
export const isSmallScreen: Readable<boolean> = derived(
screenBreakpoint,
($screenBreakPoint) =>
$screenBreakPoint === ScreenSize.Md || $screenBreakPoint === ScreenSize.Sm
)

// Update the store value when the screen is resized
if (typeof window !== 'undefined') {
window.addEventListener('resize', () => {
screenBreakpoint.set(getCurrentScreenBreakpoint())
})
}

function getCurrentScreenBreakpoint(): ScreenSize {
let breakpoint: ScreenSize = ScreenSize.Xl

// Check if window is defined (runs only in browser environment)
if (typeof window !== 'undefined') {
const width = window.innerWidth

const sortedBreakpoints = Object.keys(BREAKPOINTS).sort(
(a, b) => BREAKPOINTS[a as ScreenSize] - BREAKPOINTS[b as ScreenSize]
)

for (const key of sortedBreakpoints) {
if (width < BREAKPOINTS[key as ScreenSize]) {
breakpoint = key as ScreenSize
break
}
}
}
return breakpoint
}

0 comments on commit 153fb93

Please sign in to comment.