/
FixedPositioningExample.tsx
232 lines (220 loc) · 6.43 KB
/
FixedPositioningExample.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* eslint-disable @typescript-eslint/no-non-null-assertion */
// disabled the no-non-null-assertions since the refs are guarenteed to not be
// null by the time of running and this will eventually be re-written once I
// updated the `@react-md/transition` package for the new `nodeRef` API
import React, { ReactElement, useCallback, useRef, useState } from "react";
import CSSTransition, {
CSSTransitionClassNames,
} from "react-transition-group/CSSTransition";
import { Button } from "@react-md/button";
import {
Checkbox,
Fieldset,
Form,
ListboxOption,
Select,
useChecked,
} from "@react-md/form";
import { ArrowDropDownSVGIcon } from "@react-md/material-icons";
import { Overlay } from "@react-md/overlay";
import { useFixedPositioning } from "@react-md/transition";
import { Text } from "@react-md/typography";
import {
HorizontalPosition,
PositionAnchor,
PositionWidth,
useToggle,
VerticalPosition,
} from "@react-md/utils";
import styles from "./FixedPositioningExample.module.scss";
const horizontals: HorizontalPosition[] = [
"left",
"right",
"center",
"inner-left",
"inner-right",
];
const verticals: VerticalPosition[] = [
"above",
"below",
"center",
"top",
"bottom",
];
const widths: PositionWidth[] = ["auto", "equal", "min"];
const anchors = horizontals.reduce<Record<string, PositionAnchor>>(
(value, x) => {
verticals.forEach((y) => {
value[`${x} ${y}`] = { x, y };
});
return value;
},
{}
);
const anchorOptions = Object.entries(anchors).map(([value, anchor]) => ({
...anchor,
label: value,
value,
}));
type Anchor = typeof anchorOptions[0];
const CENTERED_ANCHOR = anchorOptions.find(
(anchor) => anchor.label === "center center"
) as Anchor;
const CLASSNAMES: CSSTransitionClassNames = {
appear: styles.enter,
appearActive: styles.entering,
enter: styles.enter,
enterActive: styles.entering,
exit: styles.exit,
exitActive: styles.exiting,
};
export default function FixedPositioningExample(): ReactElement {
const [visible, show, hide] = useToggle(false);
const buttonRef = useRef<HTMLButtonElement | null>(null);
const divRef = useRef<HTMLDivElement>(null);
const [disableSwapping, handleSwapCange] = useChecked(false);
const [transformOrigin, handleOriginChange] = useChecked(false);
const [hideOnScroll, handleScrollChange] = useChecked(true);
const [hideOnResize, handleScrollResize] = useChecked(true);
const [anchor, setAnchor] = useState(anchorOptions[0]);
const handleAnchorChange = useCallback(
(_value: string, anchor: ListboxOption) => {
setAnchor(anchor as Anchor);
},
[]
);
const [width, setWidth] = useState<PositionWidth>("auto");
const handleWidthChange = useCallback((nextWidth: string) => {
setAnchor(CENTERED_ANCHOR);
setWidth(nextWidth as PositionWidth);
}, []);
const { style, onEnter, onEntering, onEntered, onExited } =
useFixedPositioning({
fixedTo: buttonRef.current,
anchor: { x: anchor.x, y: anchor.y },
width,
transformOrigin,
disableSwapping,
onScroll(_event, { fixedTo: button }) {
if (hideOnScroll) {
hide();
return;
}
if (!button) {
return;
}
// hide when the button isn't in the viewport anymore if the
// hideOnScroll behavior is disabled
const { top } = button.getBoundingClientRect();
if (top < 0 || top > window.innerHeight) {
hide();
}
},
onResize(_event) {
if (hideOnResize) {
hide();
}
},
});
return (
<>
<Form className={styles.form}>
<Fieldset legend="Fixed Positioning Options">
<Checkbox
id="fixed-swap"
name="options"
label="Disable Swapping"
checked={disableSwapping}
onChange={handleSwapCange}
/>
<Checkbox
id="fixed-origin"
name="options"
label="Transform Origin"
checked={transformOrigin}
onChange={handleOriginChange}
/>
<Checkbox
id="fixed-hide-on-scroll"
name="options"
label="Hide on scroll"
checked={hideOnScroll}
onChange={handleScrollChange}
/>
<Checkbox
id="fixed-hide-on-resize"
name="options"
label="Hide on resize"
checked={hideOnResize}
onChange={handleScrollResize}
/>
</Fieldset>
<Select
id="fixed-anchor-type"
label="Anchor"
className={styles.select}
listboxClassName={styles.listbox}
inline
options={anchorOptions}
value={anchor.value}
onChange={handleAnchorChange}
rightChildren={<ArrowDropDownSVGIcon />}
listboxWidth="min"
isOptionDisabled={(option) => {
const opt = option as Anchor;
return width !== "auto" && !opt.value.startsWith("center");
}}
/>
<Select
id="fixed-anchor-width"
label="Fixed element width"
className={styles.select}
inline
options={widths}
value={width}
onChange={handleWidthChange}
rightChildren={<ArrowDropDownSVGIcon />}
/>
<div className={styles.footer}>
<Button
id="fixed-positioning-button"
ref={buttonRef}
onClick={show}
theme="primary"
themeType="contained"
type="submit"
>
Toggle
</Button>
</div>
</Form>
<Overlay
id="fixed-positioning-overlay"
onRequestClose={hide}
hidden
visible={visible}
/>
<CSSTransition
in={visible}
nodeRef={divRef}
mountOnEnter
unmountOnExit
classNames={CLASSNAMES}
timeout={{ enter: 200, exit: 150 }}
onEnter={(isAppearing) => onEnter(divRef.current!, isAppearing)}
onEntering={(isAppearing) => onEntering(divRef.current!, isAppearing)}
onEntered={(isAppearing) => onEntered(divRef.current!, isAppearing)}
onExited={() => onExited(divRef.current!)}
>
<div
id="fixed-position-div"
ref={divRef}
style={style}
className={styles.div}
>
<Text>This is some amazing text in a fixed element!</Text>
</div>
</CSSTransition>
</>
);
}