Skip to content

Commit

Permalink
Reduce snow motion (tldraw#5148)
Browse files Browse the repository at this point in the history
Remove snow when reduce motion is on

### Change type

- [ ] `bugfix`
- [ ] `improvement`
- [ ] `feature`
- [ ] `api`
- [x] `other`
  • Loading branch information
steveruizok authored Dec 20, 2024
1 parent a0088a0 commit 383dfd1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
8 changes: 5 additions & 3 deletions apps/dotcom/client/src/components/SnowStorm.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useRef } from 'react'
import { Vec, useEditor } from 'tldraw'
import { Vec, useEditor, usePrefersReducedMotion } from 'tldraw'

/* eslint-disable local/prefer-class-methods */
interface Snowflake {
Expand Down Expand Up @@ -148,8 +148,10 @@ class Snowstorm {
export function SnowStorm() {
const editor = useEditor()
const rElm = useRef<HTMLDivElement>(null)
const prefersReducedMotion = usePrefersReducedMotion()

useEffect(() => {
if (prefersReducedMotion) return
if (!rElm.current) return
const snowstorm = new Snowstorm(rElm.current)
const velocity = new Vec(0, 0)
Expand All @@ -161,7 +163,7 @@ export function SnowStorm() {
const time = Date.now() - start

// make wind gradually cycle between 0 and 10, maybe a bit randomly, like gusts of wind
snowstorm.baseWindX = Math.sin(time / 30_000) * 3
snowstorm.baseWindX = Math.sin(time / 30_000) * 2

const newCamera = editor.getCamera()

Expand Down Expand Up @@ -198,7 +200,7 @@ export function SnowStorm() {
editor.off('tick', updateOnTick)
snowstorm.dispose()
}
}, [editor])
}, [editor, prefersReducedMotion])

return <div ref={rElm} className="tl-snowstorm" />
}
3 changes: 3 additions & 0 deletions packages/tldraw/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -3818,6 +3818,9 @@ export function useMenuIsOpen(id: string, cb?: (isOpen: boolean) => void): reado
// @public (undocumented)
export function useNativeClipboardEvents(): void;

// @public (undocumented)
export function usePrefersReducedMotion(): boolean;

// @public (undocumented)
export function usePreloadAssets(assetUrls: TLEditorAssetUrls): {
done: boolean;
Expand Down
1 change: 1 addition & 0 deletions packages/tldraw/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// <reference types="react" />

import { registerTldrawLibraryVersion } from '@tldraw/editor'
export { usePrefersReducedMotion } from './lib/shapes/shared/usePrefersReducedMotion'
export { ColorSchemeMenu } from './lib/ui/components/ColorSchemeMenu'
export { TldrawUiDialogs } from './lib/ui/components/Dialogs'
export { TldrawUiToasts } from './lib/ui/components/Toasts'
Expand Down

0 comments on commit 383dfd1

Please sign in to comment.