This repository has been archived by the owner on Dec 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
preview-overlay.tsx
110 lines (105 loc) · 2.6 KB
/
preview-overlay.tsx
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
/**
* @module @edtr-io/editor-ui
*/
/** Comment needed because of https://github.com/christopherthielen/typedoc-plugin-external-module-name/issues/337 */
import { ScopeContext } from '@edtr-io/core'
import { styled } from '@edtr-io/ui'
import * as React from 'react'
const NoClickArea = styled.div<{ active: boolean }>(props => {
return {
pointerEvents: props.active ? 'unset' : 'none',
position: 'relative'
}
})
const Overlay = styled.div<{ active: boolean; blur: boolean }>(props => {
return {
display: props.active ? 'none' : undefined,
position: 'absolute',
width: '100%',
height: '100%',
top: 0,
backgroundColor: props.blur ? 'rgba(255,255,255,0.8)' : undefined,
zIndex: 10
}
})
const ButtonWrapper = styled.div({
width: '100%',
height: '100%',
textAlign: 'center',
display: 'flex'
})
const ActivateButton = styled.button({
pointerEvents: 'all',
color: 'white',
border: 'none',
borderRadius: '5px',
padding: '2px 10px',
textAlign: 'center',
outline: 'none',
backgroundColor: 'rgb(0,126,193)',
zIndex: 10,
margin: 'auto'
})
export const PreviewOverlay: React.FunctionComponent<PreviewOverlayProps> = props => {
const [active, setActiveState] = React.useState(false)
const { scope } = React.useContext(ScopeContext)
const setActive = React.useCallback(
(nextActive: boolean) => {
if (props.onChange) {
props.onChange(nextActive)
}
setActiveState(nextActive)
},
[props]
)
React.useEffect(() => {
if (!props.focused && active) {
setActive(false)
}
}, [props.focused, active, setActive])
return (
<NoClickArea active={active}>
<Overlay blur={props.focused} active={active}>
{props.focused ? (
<ButtonWrapper>
<ActivateButton
onClick={() => {
setActive(true)
}}
>
Aktivieren
</ActivateButton>
</ButtonWrapper>
) : null}
</Overlay>
{!props.editable ? (
<ScopeContext.Provider
value={{
scope,
editable: false
}}
>
{props.children}
</ScopeContext.Provider>
) : (
props.children
)}
{active ? (
<ButtonWrapper>
<ActivateButton
onClick={() => {
setActive(false)
}}
>
Editieren
</ActivateButton>
</ButtonWrapper>
) : null}
</NoClickArea>
)
}
interface PreviewOverlayProps {
focused: boolean
editable?: boolean
onChange?: (nextActive: boolean) => void
}