fab auto hide

This commit is contained in:
Gabe Yuan
2023-08-08 14:58:36 +08:00
parent 5041743117
commit 9c38dd8ef9
2 changed files with 77 additions and 58 deletions

View File

@@ -30,14 +30,28 @@ export default function Draggable(props) {
let x = origin.x + dx;
let y = origin.y + dy;
const { w, h } = props.windowSize;
x = limitNumber(x, 0, w - props.width);
y = limitNumber(y, 0, h - props.height);
x = limitNumber(x, -props.width / 2, w - props.width / 2);
y = limitNumber(y, -props.height / 2, h - props.height / 2);
setPosition({ x, y });
}
};
const handlePointerUp = (e) => {
setOrigin(null);
if (props.name !== "fab") {
return;
}
const { w, h } = props.windowSize;
let { x: left, y: top } = position;
const right = w - left - props.width;
const bottom = h - top - props.height;
const min = Math.min(left, top, right, bottom);
left === min && (left = -props.width / 2);
top === min && (top = -props.height / 2);
right === min && (left = w - props.width / 2);
bottom === min && (top = h - props.height / 2);
setPosition({ x: left, y: top });
};
const handleClick = (e) => {
@@ -59,8 +73,8 @@ export default function Draggable(props) {
useEffect(() => {
const { w, h } = props.windowSize;
setPosition(({ x, y }) => ({
x: limitNumber(x, 0, w - props.width),
y: limitNumber(y, 0, h - props.height),
x: limitNumber(x, -props.width / 2, w - props.width / 2),
y: limitNumber(y, -props.height / 2, h - props.height / 2),
}));
}, [props.windowSize, props.width, props.height]);
@@ -71,6 +85,10 @@ export default function Draggable(props) {
left: position.x,
top: position.y,
zIndex: 2147483647,
display: props.show ? "block" : "none",
transitionProperty: origin ? "none" : "all",
transitionDuration: "1s",
transitionDelay: "1s",
}}
onClick={handleClick}
>

View File

@@ -1,7 +1,7 @@
import Paper from "@mui/material/Paper";
import Box from "@mui/material/Box";
import Fab from "@mui/material/Fab";
import TranslateIcon from '@mui/icons-material/Translate';
import TranslateIcon from "@mui/icons-material/Translate";
import ThemeProvider from "../../hooks/Theme";
import Draggable from "./Draggable";
import IconButton from "@mui/material/IconButton";
@@ -67,65 +67,66 @@ export default function Action() {
windowSize,
width: fabWidth,
height: fabWidth,
left: window.innerWidth - fabWidth - fabWidth,
top: window.innerHeight - fabWidth - fabWidth,
left: window.innerWidth - fabWidth,
top: window.innerHeight - fabWidth,
};
return (
<StoragesProvider>
<ThemeProvider>
{showPopup ? (
<Draggable
key="pop"
{...popProps}
onStart={handleStart}
onMove={handleMove}
handler={
<Paper style={{ cursor: "move" }} elevation={3}>
<Stack
direction="row"
justifyContent="space-between"
alignItems="center"
spacing={2}
>
<Box style={{ marginLeft: 16 }}>
{process.env.REACT_APP_NAME}
</Box>
<IconButton
onClick={() => {
setShowPopup(false);
}}
>
<CloseIcon />
</IconButton>
</Stack>
</Paper>
}
>
<Paper>
<Popup />
</Paper>
</Draggable>
) : (
<Draggable
key="fab"
{...fabProps}
onStart={handleStart}
onMove={handleMove}
handler={
<Fab
color="primary"
onClick={(e) => {
if (!moved) {
setShowPopup((pre) => !pre);
}
}}
<Draggable
key="pop"
name="pop"
{...popProps}
show={showPopup}
onStart={handleStart}
onMove={handleMove}
handler={
<Paper style={{ cursor: "move" }} elevation={3}>
<Stack
direction="row"
justifyContent="space-between"
alignItems="center"
spacing={2}
>
<TranslateIcon />
</Fab>
}
/>
)}
<Box style={{ marginLeft: 16 }}>
{process.env.REACT_APP_NAME}
</Box>
<IconButton
onClick={() => {
setShowPopup(false);
}}
>
<CloseIcon />
</IconButton>
</Stack>
</Paper>
}
>
<Paper>
<Popup />
</Paper>
</Draggable>
<Draggable
key="fab"
name="fab"
{...fabProps}
show={!showPopup}
onStart={handleStart}
onMove={handleMove}
handler={
<Fab
color="primary"
onClick={(e) => {
if (!moved) {
setShowPopup((pre) => !pre);
}
}}
>
<TranslateIcon />
</Fab>
}
/>
</ThemeProvider>
</StoragesProvider>
);