Skip to content
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

Global switch for enabling/disabling animations #218

Merged
merged 3 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/flip-toolkit/src/Flipper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import onFlipKeyUpdate from './flip'
import { onFlipKeyUpdate } from './flip'
import getFlippedElementPositionsBeforeUpdate from './flip/getFlippedElementPositions/getFlippedElementPositionsBeforeUpdate'
import { assign } from './utilities'
import {
Expand Down
11 changes: 10 additions & 1 deletion packages/flip-toolkit/src/flip/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ import {
ScopedSelector
} from './animateFlippedElements/types'

let enabled = true

const disableFlip = () => (enabled = false)

const enableFlip = () => (enabled = true)

const isFlipEnabled = () => enabled

const createPortalScopedSelector =
(portalKey: string) => (selector: string) => {
return toArray(
Expand Down Expand Up @@ -73,6 +81,7 @@ const onFlipKeyUpdate = ({
onComplete,
onStart
}: OnFlipKeyUpdateArgs) => {
if (!enabled) return
const mediaQuery = window.matchMedia('(prefers-reduced-motion: reduce)')
if (mediaQuery.matches) return
const flippedElementPositionsAfterUpdate =
Expand Down Expand Up @@ -150,4 +159,4 @@ const onFlipKeyUpdate = ({
}
}

export default onFlipKeyUpdate
export { disableFlip, enableFlip, isFlipEnabled, onFlipKeyUpdate }
2 changes: 1 addition & 1 deletion packages/flip-toolkit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import * as utilities from './utilities'
import * as constants from './constants'
export { default as Flipper } from './Flipper'
export { default as getFlippedElementPositionsBeforeUpdate } from './flip/getFlippedElementPositions/getFlippedElementPositionsBeforeUpdate'
export { default as onFlipKeyUpdate } from './flip'
export * from './flip'
export { utilities, constants }
export { default as spring } from './Spring'
16 changes: 16 additions & 0 deletions packages/react-flip-toolkit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,20 @@ That means any layout styles — padding, flexbox, etc—should be appli
- [React-flip-toolkit logo](https://codepen.io/aholachek/pen/ERRpEj)
- [Using Portals](https://react-flip-toolkit-demos.surge.sh/portal)

## Global configuration

### `disableFlip()`

Global switch to disable all animations in all `Flipper` containers.

### `enableFlip()`

Global switch to (re-)enable all animations in all `Flipper` containers. Animations are enabled by default. Calling this function is needed only if animations were previously disabled with `disableFlip()`.

### `isFlipEnabled()`

Returns a boolean indicating whether animations are globally enabled or disabled.

## The Components

### `Flipper`
Expand Down Expand Up @@ -486,6 +500,8 @@ spring({
- If one of your `Flipped` components is wrapping another React component rather than a DOM element, [use a render prop to get the Flipped props](#wrapping-a-react-component) and pass down to the necessary DOM element.
- Is the element that's receiving props from `Flipped` visible in the DOM? `react-flip-toolkit` attempts to optimize performance by not animating elements that are off-screen or elements that have no width or height.
- `display:inline` elements cannot be animated. If you want an `inline` element to animate, set `display:inline-block`.
- Do you have the [prefers-reduced-motion](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-reduced-motion) setting turned on? As of v7.1.0 that setting will disable all animations.
- Make sure you didn't disable animations using `disableFlip()`. You can check if animations are enabled or disabled by calling `isFlipEnabled()`. `enableFlip()` will re-enable FLIP animations globally.

### Problem #2: Things look weird / animations aren't behaving

Expand Down
1 change: 1 addition & 0 deletions packages/react-flip-toolkit/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { disableFlip, enableFlip, isFlipEnabled } from 'flip-toolkit'
export { default as Flipper } from './Flipper'
export { default as Flipped } from './Flipped'
export { default as ExitContainer } from './ExitContainer'
Expand Down