I'm trying to create a page with a book layout, so a page with some tabs that use can expand one at a time.
Here a working example: https://codesandbox.io/s/book-layout-l28gh?file=/src/App.js:0-1419
import { useState } from "react";
const dataset = [
{ name: "A section", description: "page A" },
{ name: "B section", description: "page B" },
{ name: "C section with long title", description: "page C" },
{ name: "D section", description: "page D" }
];
export default function App() {
return <Page />;
}
function Page({}) {
const [openSection, setOpenSection] = useState(0);
return (
<div
style={{
display: "flex",
justifyContent: "center",
alignItems: "center",
height: "100vh"
}}
>
{dataset.map((datum, i) => {
const { name } = datum;
const isOpen = i === openSection;
return (
<div
key={name}
style={{
height: "100%",
backgroundColor: isOpen ? "white" : "lightgray",
border: `1px solid ${isOpen ? "white" : "black"}`,
padding: 10,
flex: 1,
flexGrow: isOpen ? 1 : 0,
transition: "all 2s ease"
}}
>
<div
style={{
cursor: "pointer",
writingMode: isOpen ? "horizontal-tb" : "vertical-rl",
transition: "all 2s ease"
}}
onClick={() => setOpenSection(i)}
>
{name}
</div>
</div>
);
})}
</div>
);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
If you test it, you can notice some problems:
- when you expand a section, the title doesn't make a smooth transition with it pass to be vertical to horizontal. It should be a smooth rotation
- sometimes, I didn't understand exactly when, when you click a title, all the card seems to get closer to each other.
- another request is make the grey area all clickable but it is obvious a problem when it is open
Why? What's the problem? It there a better approach to do a layout like this?