-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathmask.tsx
More file actions
149 lines (136 loc) · 3.83 KB
/
mask.tsx
File metadata and controls
149 lines (136 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { NativeProps, withNativeProps } from '../../utils/native-props'
import React, { useMemo, useRef, useState } from 'react'
import type { FC, ReactNode } from 'react'
import { useUnmountedRef } from 'ahooks'
import { useLockScroll } from '../../utils/use-lock-scroll'
import { useSpring, animated } from '@react-spring/web'
import {
renderToContainer,
GetContainer,
} from '../../utils/render-to-container'
import { mergeProps } from '../../utils/with-default-props'
import { useConfig } from '../config-provider'
import { ShouldRender } from '../../utils/should-render'
import {
PropagationEvent,
withStopPropagation,
} from '../../utils/with-stop-propagation'
import { useSpringVisibility } from '../../utils/use-spring-visibility'
const classPrefix = `adm-mask`
const opacityRecord = {
default: 0.55,
thin: 0.35,
thick: 0.75,
}
const colorRecord: Record<string, string> = {
black: '0, 0, 0',
white: '255, 255, 255',
}
export type MaskProps = {
visible?: boolean
onMaskClick?: (event: React.MouseEvent<HTMLDivElement, MouseEvent>) => void
destroyOnClose?: boolean
forceRender?: boolean
disableBodyScroll?: boolean
color?: 'white' | 'black' | (string & {})
opacity?: 'default' | 'thin' | 'thick' | number
getContainer?: GetContainer
afterShow?: () => void
afterClose?: () => void
stopPropagation?: PropagationEvent[]
children?: ReactNode
} & NativeProps<'--z-index'>
const defaultProps = {
visible: true,
destroyOnClose: false,
forceRender: false,
color: 'black',
opacity: 'default',
disableBodyScroll: true,
getContainer: null,
stopPropagation: ['click'],
}
export const Mask: FC<MaskProps> = p => {
const props = mergeProps(defaultProps, p)
const { locale } = useConfig()
const ref = useRef<HTMLDivElement>(null)
useLockScroll(ref, props.visible && props.disableBodyScroll)
const background = useMemo(() => {
const opacity = opacityRecord[props.opacity] ?? props.opacity
const rgb = colorRecord[props.color]
return rgb ? `rgba(${rgb}, ${opacity})` : props.color
}, [props.color, props.opacity])
const [active, setActive] = useState(props.visible)
const unmountedRef = useUnmountedRef()
const { shouldCallAfterClose } = useSpringVisibility({
visible: props.visible,
active,
setActive,
afterClose: props.afterClose,
unmountedRef,
})
const { opacity } = useSpring({
opacity: props.visible ? 1 : 0,
config: {
precision: 0.01,
mass: 1,
tension: 250,
friction: 30,
clamp: true,
},
onStart: () => {
setActive(true)
},
onRest: () => {
if (unmountedRef.current) return
if (props.visible) {
setActive(true)
props.afterShow?.()
} else if (shouldCallAfterClose()) {
setActive(false)
props.afterClose?.()
}
},
})
const node = withStopPropagation(
props.stopPropagation,
withNativeProps(
props,
<animated.div
className={classPrefix}
ref={ref}
aria-hidden
style={{
...props.style,
background,
opacity,
display: active ? undefined : 'none',
}}
onClick={(e: React.MouseEvent<HTMLDivElement, MouseEvent>) => {
if (e.target === e.currentTarget) {
props.onMaskClick?.(e)
}
}}
>
{props.onMaskClick && (
<div
className={`${classPrefix}-aria-button`}
role='button'
aria-label={locale.Mask.name}
onClick={props.onMaskClick}
/>
)}
<div className={`${classPrefix}-content`}>{props.children}</div>
</animated.div>
)
)
return (
<ShouldRender
active={active}
forceRender={props.forceRender}
destroyOnClose={props.destroyOnClose}
>
{renderToContainer(props.getContainer, node)}
</ShouldRender>
)
}