-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5468 from matuzalemsteles/issue-5421
feat: add motion reduction config
- Loading branch information
Showing
11 changed files
with
133 additions
and
93 deletions.
There are no files selected for viewing
84 changes: 0 additions & 84 deletions
84
clayui.com/content/docs/css/utilities/c-prefers-reduced-motion.md
This file was deleted.
Oops, something went wrong.
This file contains 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 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,41 @@ | ||
--- | ||
title: 'Reduced Motion' | ||
description: 'Provider component which aggregates the main Clay, Icon, and Modal.' | ||
packageNpm: '@clayui/core' | ||
--- | ||
|
||
<div class="nav-toc-absolute"> | ||
<div class="nav-toc"> | ||
|
||
- [Introduction](#introduction) | ||
- [JS](#js) | ||
- [CSS](#css) | ||
|
||
</div> | ||
</div> | ||
|
||
## Introduction | ||
|
||
Setting the motion reduction of animations can be done in two different ways in Clay, transitions that are done purely in CSS can use the `c-prefers-reduced-motion` class and transitions that are made with JS are defined using Clay's `Provider` component. | ||
|
||
## JS | ||
|
||
To remove transition from components in React, you need to declare the [`Provider`](/docs/components/provider.html) component at the root of your application and set the `reducedMotion` property to whichever option you want. | ||
|
||
- `user`: Respect user's device setting. | ||
- `always`: Enforce reduced motion. | ||
- `never`: Don't reduce motion. | ||
|
||
```jsx | ||
import {Provider} from '@clayui/core'; | ||
|
||
<Provider spritemap={spritemap} reducedMotion="user"> | ||
<App /> | ||
</Provider>; | ||
``` | ||
|
||
## CSS | ||
|
||
The class `c-prefers-reduced-motion` removes transitions from child elements. It allows the site creator to remove any CSS transition in a specific section or on the whole page without having to depend on the operating system's non-essential motion setting. | ||
|
||
It also removes `scroll-behavior: smooth` if it is enabled. You can place `c-prefers-reduced-motion` on whatever element has the CSS property `scroll-behavior: smooth` to remove it. If this class is placed on the `html` element, it will remove all CSS transitions for the page. |
This file contains 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 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 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 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 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 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 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 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,48 @@ | ||
/** | ||
* SPDX-FileCopyrightText: © 2021 Liferay, Inc. <https://liferay.com> | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
import {useEffect, useState} from 'react'; | ||
|
||
export function useReducedMotion(mode: 'user' | 'always' | 'never') { | ||
const [reduceMotion, setReduceMotion] = useState(false); | ||
|
||
useEffect(() => { | ||
switch (mode) { | ||
case 'always': | ||
setReduceMotion(true); | ||
break; | ||
case 'never': | ||
break; | ||
default: { | ||
if (window.matchMedia) { | ||
const motionMediaQuery = window.matchMedia( | ||
'(prefers-reduced-motion)' | ||
); | ||
|
||
const setReducedMotionPreferences = ( | ||
mediaQuery: MediaQueryListEvent | ||
) => setReduceMotion(mediaQuery.matches); | ||
|
||
motionMediaQuery.addEventListener( | ||
'change', | ||
setReducedMotionPreferences | ||
); | ||
|
||
return () => { | ||
motionMediaQuery.removeEventListener( | ||
'change', | ||
setReducedMotionPreferences | ||
); | ||
}; | ||
} else { | ||
setReduceMotion(false); | ||
} | ||
break; | ||
} | ||
} | ||
}, []); | ||
|
||
return reduceMotion; | ||
} |