Sticky Columns Demo
by Dave Stewart
HTML
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://cdn.tailwindcss.com"></script>
<div id="root"></div>
CSS
body {
margin: 0;
font-family: system-ui, sans-serif;
}
React
// JavaScript pane (set language to Babel):
const generateDummyText = () => {
const texts = [
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
"Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.",
"Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
];
return texts.slice(0, Math.floor(Math.random() * 3) + 2);
};
const StickySection = ({ columnIndex, sectionIndex }) => {
const headerRef = React.useRef(null);
const subheaderRef = React.useRef(null);
const contentHeaderRef = React.useRef(null);
const [subheaderTop, setSubheaderTop] = React.useState(0);
const [contentHeaderTop, setContentHeaderTop] = React.useState(0);
const colors = {
0: { header: 'bg-red-100', subheader: 'bg-red-50' },
1: { header: 'bg-blue-100', subheader: 'bg-blue-50' },
2: { header: 'bg-green-100', subheader: 'bg-green-50' },
3: { header: 'bg-purple-100', subheader: 'bg-purple-50' },
4: { header: 'bg-yellow-100', subheader: 'bg-yellow-50' },
};
const colorSet = colors[columnIndex];
React.useEffect(() => {
const updateStickyPositions = () => {
if (headerRef.current && subheaderRef.current && contentHeaderRef.current) {
const headerHeight = headerRef.current.offsetHeight;
setSubheaderTop(headerHeight);
const subheaderHeight = subheaderRef.current.offsetHeight;
setContentHeaderTop(headerHeight + subheaderHeight);
}
};
updateStickyPositions();
window.addEventListener('resize', updateStickyPositions);
return () => window.removeEventListener('resize', updateStickyPositions);
}, []);
return (
<div className="mb-6 bg-white rounded-lg overflow-clip">
<div
...