overlay reduced motion support

This commit is contained in:
tmshv committed 2019-10-15 23:05:31 +03:00
1 parent bdde0f79c1
commit fbcf204642
3 files changed
+15 -1

No files matched your search

+5 -1
View File
@@ -1,6 +1,7 @@
import * as React from 'react' import * as React from 'react'
export interface ITransitionProps { export interface ITransitionProps {
duration: number
extraStyle?: React.CSSProperties extraStyle?: React.CSSProperties
show: boolean show: boolean
} }
@@ -13,6 +14,9 @@ export const Transition: React.FC<ITransitionProps> = props => {
setMounted(false) setMounted(false)
} }
}, [opacity]) }, [opacity])
const transition = React.useMemo(
() => `all ${props.duration}ms`,
[props.duration])
React.useEffect(() => { React.useEffect(() => {
if (!props.show) { if (!props.show) {
@@ -35,7 +39,7 @@ export const Transition: React.FC<ITransitionProps> = props => {
style={{ style={{
...props.extraStyle, ...props.extraStyle,
opacity, opacity,
transition: 'all 250ms', transition,
}} }}
onTransitionEnd={onTransitionEnd} onTransitionEnd={onTransitionEnd}
> >
+5
View File
@@ -1,5 +1,6 @@
import * as React from 'react' import * as React from 'react'
import { Transition } from './Transition' import { Transition } from './Transition'
import { useReducedMotion } from '../../hooks/useReducedMotion'
export interface IOverlayProps { export interface IOverlayProps {
show: boolean show: boolean
@@ -7,6 +8,9 @@ export interface IOverlayProps {
} }
export const Overlay: React.FC<IOverlayProps> = props => { export const Overlay: React.FC<IOverlayProps> = props => {
const motionDisabled = useReducedMotion()
const duration = motionDisabled ? 0 : 250
const onClick = React.useCallback((event: React.MouseEvent<HTMLDivElement>) => { const onClick = React.useCallback((event: React.MouseEvent<HTMLDivElement>) => {
if (event.target === event.currentTarget) { if (event.target === event.currentTarget) {
props.onClickOverlay() props.onClickOverlay()
@@ -19,6 +23,7 @@ export const Overlay: React.FC<IOverlayProps> = props => {
return ( return (
<Transition <Transition
duration={duration}
show={props.show} show={props.show}
extraStyle={{ extraStyle={{
zIndex: 1000000, zIndex: 1000000,
+5
View File
@@ -0,0 +1,5 @@
import useMedia from 'use-media'
export function useReducedMotion(): boolean {
return useMedia('(prefers-reduced-motion: reduce)')
}