Skip to content

Commit

Permalink
minor updates to css reset and layout
Browse files Browse the repository at this point in the history
  • Loading branch information
arzafran committed Nov 20, 2024
1 parent b70062e commit 7432ef1
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 186 deletions.
23 changes: 17 additions & 6 deletions hooks/use-device-detection.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
import { useMediaQuery } from '@darkroom.engineering/hamo'
import { useOrchestra } from '~/libs/orchestra/react'
import { breakpoints } from '~/styles/config.mjs'

interface NavigatorWithBattery extends Navigator {
getBattery(): Promise<{ charging: boolean; level: number }>
}

export function useDeviceDetection() {
const breakpoint = breakpoints.dt

const { webgl } = useOrchestra()

const isMobile = useMediaQuery(`(max-width: ${breakpoint - 1}px)`)
const isDesktop = useMediaQuery(`(min-width: ${breakpoint}px)`)
const isReducedMotion = useMediaQuery('(prefers-reduced-motion: reduce)')
const isWebGL = isDesktop && !isReducedMotion && webgl
// TODO: const isLowPowerMode
const isWebGL = isDesktop && !isReducedMotion

// test thoroughly
const isLowPowerMode =
useMediaQuery('(any-pointer: coarse) and (hover: none)') &&
'getBattery' in navigator &&
(navigator as NavigatorWithBattery)
.getBattery()
.then(
(battery: { charging: boolean; level: number }) =>
battery.charging === false && battery.level <= 0.2
)

return { isMobile, isDesktop, isReducedMotion, isWebGL }
return { isMobile, isDesktop, isReducedMotion, isWebGL, isLowPowerMode }
}
84 changes: 0 additions & 84 deletions hooks/use-fouc-fix.ts

This file was deleted.

15 changes: 11 additions & 4 deletions hooks/use-framerate.js → hooks/use-framerate.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import { useFrame } from '@darkroom.engineering/hamo'
import { useRef } from 'react'

export function useFramerate(fps, callback, priority = 0) {
type FPSValue = number | (() => number)
type FrameCallback = (time: number, deltaTime: number) => void

export function useFramerate(
fps: FPSValue,
callback?: FrameCallback,
priority = 0
): void {
const timeRef = useRef(0)

useFrame((time, delaTime) => {
timeRef.current += delaTime
useFrame((time, deltaTime) => {
timeRef.current += deltaTime

const executionTime = 1000 / (typeof fps === 'function' ? fps() : fps)

if (timeRef.current >= executionTime) {
timeRef.current = timeRef.current % executionTime
callback?.(time, delaTime)
callback?.(time, deltaTime)
}
}, priority)
}
31 changes: 0 additions & 31 deletions hooks/use-lazy-state.js

This file was deleted.

40 changes: 40 additions & 0 deletions hooks/use-lazy-state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { useCallback, useEffect, useRef } from 'react'

type SetStateAction<T> = T | ((prevState: T) => T)
type StateCallback<T> = (value: T, prevValue: T) => void

export function useLazyState<T>(
initialValue: T,
callback: StateCallback<T>,
deps: unknown[] = []
) {
const stateRef = useRef<T>(initialValue)

// biome-ignore lint/correctness/useExhaustiveDependencies: we need to trigger on initialValue change
useEffect(() => {
callback(initialValue, initialValue)
}, [initialValue])

function set(value: SetStateAction<T>) {
if (typeof value === 'function') {
const nextValue = (value as (prevState: T) => T)(stateRef.current)
callback(nextValue, stateRef.current)
stateRef.current = nextValue
return
}

if (value !== stateRef.current) {
callback(value, stateRef.current)
stateRef.current = value
}
}

const get = useCallback(() => stateRef.current, [])

// biome-ignore lint/correctness/useExhaustiveDependencies: we need to trigger on deps change
useEffect(() => {
callback(stateRef.current, stateRef.current)
}, [...deps])

return [get, set] as const
}
44 changes: 20 additions & 24 deletions styles/_layout.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,53 @@
--desktop-columns-gap: desktop-vw(16px);
--mobile-margin: mobile-vw(16px);
--desktop-margin: desktop-vw(16px);
}

/* */
/* THERE SHOULD BE NO NEED TO TOUCH BEYOND THIS POINT,
/* JUST CONFIGURE THE SETTINGS ABOVE
/* */
/* */
/* THERE SHOULD BE NO NEED TO TOUCH BEYOND THIS POINT,
/* JUST CONFIGURE THE SETTINGS ABOVE
/* */

:root {
--layout-columns-count: var(--mobile-columns-count);
--layout-columns-gap: var(--mobile-columns-gap);
--layout-margin: var(--mobile-margin);
--layout-width: calc(calc(100 * var(--vw, 1vw)) - (2 * var(--layout-margin)));
--layout-width: calc(100vw - (2 * var(--layout-margin)));
--layout-column-width: calc(
(
var(--layout-width) -
((var(--layout-columns-count) - 1) * var(--layout-columns-gap))
(var(--layout-columns-count) - 1) *
var(--layout-columns-gap)
) /
var(--layout-columns-count)
);
}

@include-media ('desktop') {
@include-media ('desktop') {
:root {
--layout-columns-count: var(--desktop-columns-count);
--layout-columns-gap: var(--desktop-columns-gap);
--layout-margin: var(--desktop-margin);
}
}

.grid {
display: grid;
grid-template-columns: repeat(var(--layout-columns-count), 1fr);
gap: var(--layout-columns-gap);
}

.layout-block {
margin-left: auto;
margin-right: auto;
margin-inline: auto;
width: calc(100% - 2 * var(--layout-margin));
}

.layout-block-inner {
padding-left: var(--layout-margin);
padding-right: var(--layout-margin);
padding-inline: var(--layout-margin);
width: 100%;
}

.grid {
display: grid;
grid-template-columns: repeat(var(--layout-columns-count), minmax(0, 1fr));
gap: var(--layout-columns-gap);
}

.layout-grid {
@extend .layout-block;
@extend .grid;
@extend .layout-block, .grid;
}

.layout-grid-inner {
@extend .layout-block-inner;
@extend .grid;
@extend .layout-block-inner, .grid;
}
44 changes: 39 additions & 5 deletions styles/_reset.css
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
svg,
video,
audio,
#theatrejs-studio-root,
vercel-live-feedback,
nextjs-portal
nextjs-portal,
dialog,
:where(select, option)
):not(svg *, symbol *)
) {
all: unset;
Expand Down Expand Up @@ -91,14 +92,14 @@ meter {
}

/* fix the feature of 'hidden' attribute.
display:revert; revert to element instead of attribute */
display:revert; revert to element instead of attribute */
:where([hidden]) {
display: none;
}

/* revert for bug in Chromium browsers
- fix for the content editable attribute will work properly.
- webkit-user-select: auto; added for Safari in case of using user-select:none on wrapper element*/
- fix for the content editable attribute will work properly.
- webkit-user-select: auto; added for Safari in case of using user-select:none on wrapper element*/
:where([contenteditable]:not([contenteditable="false"])) {
-moz-user-modify: read-write;
-webkit-user-modify: read-write;
Expand All @@ -118,3 +119,36 @@ meter {
:where(dialog:modal) {
all: revert;
}

/* Add support for color-scheme */
:root {
color-scheme: light dark;
}

/* Add support for newer form controls */
:where(input[type="file"]) {
cursor: default;
-webkit-appearance: none;
appearance: none;
}

/* Improve text rendering */
html {
text-size-adjust: 100%;
-webkit-text-size-adjust: 100%;
-moz-text-size-adjust: 100%;
text-rendering: optimizeLegibility;
}

/* Remove tap highlight on iOS */
a,
button {
-webkit-tap-highlight-color: transparent;
}

/* Improve media defaults */
video,
audio {
max-inline-size: 100%;
max-block-size: 100%;
}
Loading

1 comment on commit 7432ef1

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚡️ Lighthouse report for the changes in this commit:

🟢 Performance: 99
🟢 Accessibility: 90
🟢 Best practices: 96
🟠 SEO: 63

Lighthouse ran on https://satus-legkgbkt3-darkroom-engineering.vercel.app/

Please sign in to comment.