Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 677
feat(Spinner): add support for synchronized animations#7157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
dc950de
feat(Spinner): add support for synchronized animations
joshblack e97adba
refactor: clean up code, add comments
joshblack 9332c33
chore: add changeset
joshblack 4e66bc5
refactor: add dependencies to callback
joshblack 6ecc8cb
refactor: add cleanup code and safer fallback
joshblack 3ea54c0
chore: update snapshots
joshblack 6db3be9
docs: add comment to hook helper
joshblack 4c1eb71
Merge branch 'main' of github.com:primer/react into feat/add-synchron…
joshblack 19975e0
refactor(Spinner): update animation to reuse existing css for startTime
joshblack e3d5dd6
chore: fix lint violations
joshblack f89e7f0
Merge branch 'main' of github.com:primer/react into feat/add-synchron…
joshblack c6eaacf
test: update snapshots
joshblack 9e4a8d1
refactor: wrap behavior behind feature flag
joshblack b21ee3c
chore: update changeset
joshblack File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@primer/react': minor | ||
| --- | ||
| Add feature flag to control whether Spinner animations are synchronized |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,12 @@ | ||
| import {clsx} from 'clsx' | ||
| import type React from 'react' | ||
| import {useState, useEffect} from 'react' | ||
| import {useCallback, useEffect, useRef, useState, useSyncExternalStore} from 'react' | ||
| import {VisuallyHidden} from '../VisuallyHidden' | ||
| import type {HTMLDataAttributes} from '../internal/internal-types' | ||
| import {useId} from '../hooks' | ||
| import classes from './Spinner.module.css' | ||
| import {clsx} from 'clsx' | ||
| import {useMedia} from '../hooks/useMedia' | ||
| import {useFeatureFlag} from '../FeatureFlags' | ||
| const sizeMap = { | ||
| small: '16px', | ||
| @@ -34,6 +36,8 @@ function Spinner({ | ||
| delay = false, | ||
| ...props | ||
| }: SpinnerProps) { | ||
| const syncAnimationsEnabled = useFeatureFlag('primer_react_spinner_synchronize_animations') | ||
| const animationRef = useSpinnerAnimation() | ||
| const size = sizeMap[sizeKey] | ||
| const hasHiddenLabel = srText !== null && ariaLabel === undefined | ||
| const labelId = useId() | ||
| @@ -58,6 +62,7 @@ function Spinner({ | ||
| /* inline-flex removes the extra line height */ | ||
| <span className={classes.Box}> | ||
| <svg | ||
| ref={syncAnimationsEnabled ? animationRef : undefined} | ||
| height={size} | ||
| width={size} | ||
| viewBox="0 0 16 16" | ||
| @@ -93,4 +98,127 @@ function Spinner({ | ||
| Spinner.displayName = 'Spinner' | ||
| type Subscriber = () => void | ||
| type AnimationTimingValue = { | ||
| startTime: CSSNumberish | null | ||
| } | ||
| type AnimationTimingStore = { | ||
| subscribers: Set<Subscriber> | ||
| value: AnimationTimingValue | ||
| update(startTime: CSSNumberish): void | ||
| subscribe(subscriber: Subscriber): () => void | ||
| getSnapshot(): AnimationTimingValue | ||
| getServerSnapshot(): AnimationTimingValue | ||
| } | ||
| const animationTimingStore: AnimationTimingStore = { | ||
| subscribers: new Set<() => void>(), | ||
| value: { | ||
| startTime: null, | ||
| }, | ||
| update(startTime) { | ||
| const value = { | ||
| startTime, | ||
| } | ||
| animationTimingStore.value = value | ||
| for (const subscriber of animationTimingStore.subscribers) { | ||
| subscriber() | ||
| } | ||
| }, | ||
| subscribe(subscriber) { | ||
| animationTimingStore.subscribers.add(subscriber) | ||
| return () => { | ||
| animationTimingStore.subscribers.delete(subscriber) | ||
| } | ||
| }, | ||
| getSnapshot() { | ||
| return animationTimingStore.value | ||
| }, | ||
| getServerSnapshot() { | ||
| return animationTimingStore.value | ||
| }, | ||
| } | ||
| /** | ||
| * A utility hook for reading a common `startTime` value so that all animations | ||
| * are in sync. This is a global value and is coordinated through `useSyncExternalStore`. | ||
| */ | ||
| function useAnimationTiming() { | ||
| return useSyncExternalStore( | ||
| animationTimingStore.subscribe, | ||
| animationTimingStore.getSnapshot, | ||
| animationTimingStore.getServerSnapshot, | ||
| ) | ||
| } | ||
| /** | ||
| * Uses a technique from Spectrum to coordinate animations: | ||
| * @see https://github.com/adobe/react-spectrum/blob/ab5e6f3dba4235dafab9f81f8b5c506ce5f11230/packages/%40react-spectrum/s2/src/Skeleton.tsx#L21 | ||
| */ | ||
| function useSpinnerAnimation() { | ||
| const ref = useRef<Animation | null>(null) | ||
| const noMotionPreference = useMedia('(prefers-reduced-motion: no-preference)', false) | ||
| const animationTiming = useAnimationTiming() | ||
| return useCallback( | ||
| (element: HTMLElement | SVGSVGElement | null) => { | ||
| if (!element) { | ||
| return | ||
| } | ||
| if (ref.current !== null) { | ||
| return | ||
| } | ||
| if (noMotionPreference) { | ||
| const cssAnimation = element.getAnimations().find((animation): animation is CSSAnimation => { | ||
| if (animation instanceof CSSAnimation) { | ||
| return animation.animationName.startsWith('Spinner') && animation.animationName.endsWith('rotate-keyframes') | ||
| } | ||
| return false | ||
| }) | ||
| // If we can find a CSS Animation, pause it and we will use the Web | ||
| // Animations API to pick up from where it left off | ||
| cssAnimation?.pause() | ||
| ref.current = element.animate( | ||
| [ | ||
| { | ||
| transform: 'rotate(0deg)', | ||
| }, | ||
| { | ||
| transform: 'rotate(360deg)', | ||
| }, | ||
| ], | ||
| { | ||
| // var(--base-duration-1000) | ||
| duration: 1000, | ||
| // var(--base-easing-linear) | ||
| easing: 'cubic-bezier(0,0,1,1)', | ||
| iterations: Infinity, | ||
| }, | ||
| ) | ||
| // When the `startTime` value from `animationTimingStore` is `null` we | ||
| // are currently hydrating on the client. In this case, the first | ||
| // spinner to mount will set the `startTime` for all other spinners. | ||
| if (animationTiming.startTime === null) { | ||
| const startTime = cssAnimation?.startTime ?? 0 | ||
| animationTimingStore.update(startTime) | ||
| // We use `startTime` to sync different animations. When all animations | ||
| // have the same startTime they will be in sync. | ||
| // @see https://developer.mozilla.org/en-US/docs/Web/API/Animation/startTime#syncing_different_animations | ||
| ref.current.startTime = startTime | ||
| } else { | ||
| ref.current.startTime = animationTiming.startTime | ||
| } | ||
| } | ||
joshblack marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| }, | ||
| [noMotionPreference, animationTiming], | ||
| ) | ||
| } | ||
| export default Spinner | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.