-
Notifications
You must be signed in to change notification settings - Fork 582
/
EditorResponsiveBar.tsx
191 lines (180 loc) · 6.7 KB
/
EditorResponsiveBar.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import React, { useCallback, useEffect, useMemo } from "react";
import { useRecoilState } from "recoil";
import { css } from "emotion";
import classNames from "classnames";
import { plugins } from "@webiny/plugins";
import { IconButton } from "@webiny/ui/Button";
import { Tooltip } from "@webiny/ui/Tooltip";
import { Typography } from "@webiny/ui/Typography";
import { PbEditorResponsiveModePlugin } from "~/types";
import { uiAtom, setDisplayModeMutation } from "../../../recoil/modules";
import { usePageBuilder } from "~/hooks/usePageBuilder";
import { usePageElements } from "@webiny/app-page-builder-elements/hooks/usePageElements";
import { isPerBreakpointStylesObject } from "@webiny/app-page-builder-elements/utils";
const classes = {
wrapper: css({
height: "100%",
display: "flex",
justifyContent: "center",
alignItems: "center",
"& .action-wrapper": {
height: "100%",
display: "flex",
justifyContent: "center",
alignItems: "center",
borderRight: "1px solid var(--mdc-theme-background)",
"&:first-child": {
borderLeft: "1px solid var(--mdc-theme-background)"
},
"&.active": {
backgroundColor: "var(--mdc-theme-background)",
"& .mdc-icon-button": {
color: "var(--mdc-theme-text-primary-on-background)"
}
}
}
}),
dimensionIndicator: css({
height: "100%",
display: "flex",
alignItems: "center",
padding: "0px 16px",
borderRight: "1px solid var(--mdc-theme-background)",
"& span": {
color: "var(--mdc-theme-text-primary-on-background)"
},
"& .width": {
marginRight: 8,
"& span:last-child": {
marginLeft: 4
}
},
"& .height": {
"& span:last-child": {
marginLeft: 4
}
}
}),
tooltip: css({
textAlign: "left",
textTransform: "initial",
"& .tooltip__title": {
"& span": {
fontWeight: 900
}
},
"& .tooltip__info": {
display: "flex",
"& span": {
fontWeight: 600
},
"& svg": {
fill: "var(--mdc-theme-surface)",
marginRight: 4
}
},
"& .tooltip__body": {
marginTop: 4,
"& span": {
fontWeight: 600
}
}
})
};
const EditorResponsiveBar = () => {
const [{ displayMode, pagePreviewDimension }, setUiAtomValue] = useRecoilState(uiAtom);
const {
responsiveDisplayMode: { setDisplayMode }
} = usePageBuilder();
const setEditorMode = useCallback(
displayMode => {
setUiAtomValue(prev => setDisplayModeMutation(prev, displayMode));
/**
* We are updating the "displayMode" in PageBuilder context.
* Because "ElementRoot" needs its value to apply "visibility" element style setting.
*/
setDisplayMode(displayMode);
},
[uiAtom]
);
const editorModes = useMemo(
() => plugins.byType<PbEditorResponsiveModePlugin>("pb-editor-responsive-mode"),
[]
);
const pageElements = usePageElements();
if (pageElements) {
// By default, we want to only assign styles for the first breakpoint in line, which is "desktop".
// We only care about tablet, mobile-landscape, and mobile-portrait if user selects one of those.
useEffect(() => {
pageElements.setAssignStylesCallback(params => {
const whitelistedBreakpoints = [];
for (let i = 0; i < editorModes.length; i++) {
const current = editorModes[i];
whitelistedBreakpoints.push(current.config.displayMode);
if (current.config.displayMode === displayMode) {
break;
}
}
const { breakpoints, styles = {}, assignTo = {} } = params;
if (isPerBreakpointStylesObject({ breakpoints, styles })) {
for (const breakpointName in breakpoints) {
if (
styles[breakpointName] &&
whitelistedBreakpoints.includes(breakpointName)
) {
Object.assign(assignTo, styles[breakpointName]);
}
}
} else {
Object.assign(assignTo, styles);
}
return assignTo;
});
}, [displayMode]);
}
const responsiveBarContent = useMemo(() => {
return editorModes.map(({ config: { displayMode: mode, icon, toolTip } }) => {
return (
<Tooltip
key={mode}
content={
<div className={classes.tooltip}>
<div className={"tooltip__title"}>
<Typography use={"subtitle1"}>{toolTip.title}</Typography>
</div>
<div className={"tooltip__info"}>
{toolTip.subTitleIcon}
<Typography use={"body2"}>{toolTip.subTitle}</Typography>
</div>
<div className={"tooltip__body"}>
<Typography use={"body2"}>{toolTip.body}</Typography>
</div>
</div>
}
placement={"bottom"}
className={classNames("action-wrapper", {
active: mode === displayMode
})}
>
<IconButton icon={icon} onClick={() => setEditorMode(mode)} />
</Tooltip>
);
});
}, [setEditorMode, displayMode]);
return (
<div className={classes.wrapper}>
{responsiveBarContent}
<div className={classes.dimensionIndicator}>
<span className="width">
<Typography use={"subtitle2"}>{pagePreviewDimension.width}</Typography>
<Typography use={"subtitle2"}>PX</Typography>
</span>
<span className="height">
<Typography use={"subtitle2"}>{"100"}</Typography>
<Typography use={"subtitle2"}>%</Typography>
</span>
</div>
</div>
);
};
export default EditorResponsiveBar;