Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: right sidebar should trigger toolbar when scrolled #2325

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions src/js/components/AppToolbar/AppToolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,27 +185,36 @@ export const AppToolbar = (props: AppToolbarProps): React.ReactElement => {
} = props;
const { colorMode, toggleColorMode } = useColorMode();
const [canShowFullSecondaryMenu] = useMediaQuery('(min-width: 900px)');
const [isScrolledPastTitle, setIsScrolledPastTitle] = React.useState(null);
const {
toolbarRef,
toolbarHeight,
mainSidebarWidth,
isScrolledPastTitle,
setIsScrolledPastTitle
} = React.useContext(LayoutContext);

const toast = useToast();
const commentsToggleToastRef = React.useRef(null);

// add event listener to detect when the user scrolls past the title
React.useLayoutEffect(() => {
const scrollContainer = document.getElementById("main-layout") as HTMLElement;

const handleScroll = () => {
if (scrollContainer.scrollTop > PAGE_TITLE_SHOW_HEIGHT) {
setIsScrolledPastTitle(true);
} else {
setIsScrolledPastTitle(false);
if (scrollContainer) {
const handleScroll = () => {
if (scrollContainer.scrollTop > PAGE_TITLE_SHOW_HEIGHT) {
setIsScrolledPastTitle(prev => ({ ...prev, "mainContent": true }));
} else {
setIsScrolledPastTitle(prev => ({ ...prev, "mainContent": false }));
}
}
handleScroll();
scrollContainer.addEventListener('scroll', handleScroll);
return () => scrollContainer.removeEventListener('scroll', handleScroll);
}
handleScroll();
scrollContainer.addEventListener('scroll', handleScroll);
return () => scrollContainer.removeEventListener('scroll', handleScroll);
}, []);

const shouldShowUnderlay = Object.values(isScrolledPastTitle).some(x => x);

// If the workspace color mode doesn't match
// the chakra color mode, update the chakra color mode
React.useEffect(() => {
Expand All @@ -216,12 +225,6 @@ export const AppToolbar = (props: AppToolbarProps): React.ReactElement => {
}
}, [isThemeDark, toggleColorMode]);

const {
toolbarRef,
toolbarHeight,
mainSidebarWidth
} = React.useContext(LayoutContext);

const secondaryTools = [
handleClickComments && {
label: isShowComments ? "Hide comments" : "Show comments",
Expand Down Expand Up @@ -359,7 +362,7 @@ export const AppToolbar = (props: AppToolbarProps): React.ReactElement => {
>
{currentPageTitle && (
<LocationIndicator
isVisible={isScrolledPastTitle}
isVisible={shouldShowUnderlay}
type="node"
uid="123"
title={currentPageTitle}
Expand Down Expand Up @@ -415,7 +418,7 @@ export const AppToolbar = (props: AppToolbarProps): React.ReactElement => {
{contentControls || <Spacer />}
{rightToolbarControls}

{(isScrolledPastTitle && (
{(shouldShowUnderlay && (
<Box
as={motion.div}
key="header-backdrop"
Expand All @@ -441,7 +444,7 @@ export const AppToolbar = (props: AppToolbarProps): React.ReactElement => {
variants={variants}
animate={[
isLeftSidebarOpen && "isLeftSidebarOpen",
isScrolledPastTitle && "visible"
shouldShowUnderlay && "visible"
].filter(Boolean)}
exit={{ opacity: 0 }}
/>
Expand Down
39 changes: 30 additions & 9 deletions src/js/components/Layout/RightSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { LayoutContext, layoutAnimationProps } from "./useLayoutState";
import { AnimatePresence, motion } from 'framer-motion';
import { XmarkIcon, ChevronRightIcon, PageIcon, PageFillIcon, BlockIcon, BlockFillIcon, GraphIcon, ArrowLeftOnBoxIcon } from '@/Icons/Icons';
import { Button, IconButton, Box, Collapse, VStack, BoxProps } from '@chakra-ui/react';
import { useInView } from 'react-intersection-observer';

/** Right Sidebar */

Expand All @@ -14,11 +15,21 @@ interface RightSidebarProps extends BoxProps {

export const RightSidebar = (props: RightSidebarProps) => {
const { children, rightSidebarWidth, isOpen } = props;

const {
toolbarHeight
toolbarHeight,
setIsScrolledPastTitle,
} = React.useContext(LayoutContext);

const { ref: markerRef, inView } = useInView({ threshold: 0 });

React.useEffect(() => {
if (inView) {
setIsScrolledPastTitle(prev => ({ ...prev, "rightSidebar": false }));
} else {
setIsScrolledPastTitle(prev => ({ ...prev, "rightSidebar": true }));
}
}, [inView, setIsScrolledPastTitle]);

return (
<AnimatePresence initial={false}>
{isOpen && (
Expand All @@ -35,12 +46,22 @@ export const RightSidebar = (props: RightSidebarProps) => {
borderLeft="1px solid"
borderColor="separator.divider"
position="fixed"
id="right-sidebar"
height="100vh"
inset={0}
pt={`calc(${toolbarHeight} + 1rem)`}
left="auto"
>
<Box width={rightSidebarWidth + "vw"}>
<Box
bg="green"
aria-hidden
position="absolute"
ref={markerRef}
height="20px"
top={0}
/>
<Box
width={rightSidebarWidth + "vw"}>
{children}
</Box>
</Box>
Expand Down Expand Up @@ -93,11 +114,11 @@ export const SidebarItem = ({ title, type, isOpen, onToggle, onRemove, onNavigat
sx={{ maskImage: "linear-gradient(to right, black, black calc(100% - 1rem), transparent calc(100%))" }}
>
{<ChevronRightIcon
transform={isOpen ? "rotate(90deg)" : null}
transitionProperty="common"
transitionDuration="0.15s"
transitionTimingFunction="ease-in-out"
justifySelf="center" />}
transform={isOpen ? "rotate(90deg)" : null}
transitionProperty="common"
transitionDuration="0.15s"
transitionTimingFunction="ease-in-out"
justifySelf="center" />}
{typeIcon(type, isOpen)}
<Box
flex="1 1 100%"
Expand Down Expand Up @@ -148,7 +169,7 @@ export const SidebarItem = ({ title, type, isOpen, onToggle, onRemove, onNavigat
unmountOnExit
zIndex={1}
px={4}
onPointerDown={(e) => {e.stopPropagation()}}
onPointerDown={(e) => { e.stopPropagation() }}
>
{children}
</Box>
Expand Down
3 changes: 3 additions & 0 deletions src/js/components/Layout/useLayoutState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ export const useLayoutState = () => {
const mainContentRef = React.useRef();
const toolbarRef = React.useRef();
const [mainSidebarWidth, setMainSidebarWidth] = React.useState(300);
const [isScrolledPastTitle, setIsScrolledPastTitle] = React.useState({});
const toolbarHeight = "3rem";

return {
mainSidebarWidth,
setMainSidebarWidth,
isScrolledPastTitle,
setIsScrolledPastTitle,
toolbarHeight,
mainContentRef,
toolbarRef,
Expand Down