-
Notifications
You must be signed in to change notification settings - Fork 9
Sidebar/search performance #321
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2689710
Improve Performance and accessibility of sidebar
Polleps 494870b
Search in current drive instead of current server
Polleps a3b043a
Fix sidebar item doesn't open when navigating to it's sub resource
Polleps 71ec98c
Add changelog entry and errorHandler
Polleps File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,137 +1,54 @@ | ||
import React, { useCallback, useEffect, useLayoutEffect, useRef } from 'react'; | ||
import React, { useEffect, useState } from 'react'; | ||
import styled from 'styled-components'; | ||
import { timeoutEffect, timeoutEffects } from '../helpers/timeoutEffect'; | ||
import { timeoutEffect } from '../helpers/timeoutEffect'; | ||
import { animationDuration } from '../styling'; | ||
|
||
export interface CollapseProps { | ||
open: boolean; | ||
/** Animation speed in ms, Defaults to the animation duration defined in the theme */ | ||
duration?: number; | ||
interface CollapseProps { | ||
open?: boolean; | ||
className?: string; | ||
} | ||
|
||
const SPEED_MODIFIER = 1.5; | ||
// the styling file is not loaded at boot so we have to use a function here | ||
const ANIMATION_DURATION = () => animationDuration * 1.5; | ||
|
||
/** | ||
* Collapsible Component that only shows `children` when `open={true}`. Animates | ||
* the transition for `{duration}`ms. Designed to work in the sidebar with | ||
* potentially deeply nested, recurring Collapse units. | ||
*/ | ||
export function Collapse({ | ||
open, | ||
children, | ||
duration, | ||
className, | ||
children, | ||
}: React.PropsWithChildren<CollapseProps>): JSX.Element { | ||
const node = useRef<HTMLDivElement>(null); | ||
const [initialHeight, setInitialHeight] = React.useState(0); | ||
|
||
// We can't use a `useState` here. | ||
// When `measureAndSet` is used as a callback for the MutationObserver, | ||
// the scope will be outdated when the props update. | ||
// To work around this we have to use a ref in order to reference the most up to date value. | ||
const openRef = useRef(open); | ||
|
||
const measureAndSet = () => { | ||
const div = node.current; | ||
|
||
if (!div) { | ||
return; | ||
} | ||
|
||
div.style.height = 'inital'; | ||
const height = div.scrollHeight; | ||
|
||
setInitialHeight(height); | ||
|
||
div.style.position = 'static'; | ||
|
||
if (!openRef.current) { | ||
div.style.height = '0px'; | ||
div.style.visibility = 'hidden'; | ||
} else { | ||
div.style.height = `${height}px`; | ||
div.style.visibility = 'visible'; | ||
} | ||
}; | ||
|
||
const mutationObserver = useRef(new MutationObserver(measureAndSet)); | ||
const [mountChildren, setMountChildren] = useState(open); | ||
|
||
const speed = duration ?? animationDuration * SPEED_MODIFIER; | ||
|
||
// Measure the height of the element when it is added to the DOM. | ||
const onRefConnect = useCallback((div: HTMLDivElement) => { | ||
if (!div) return; | ||
|
||
// @ts-ignore this works and is mutable, ts doesn't like it | ||
node.current = div; | ||
measureAndSet(); | ||
}, []); | ||
|
||
// Measure the height again when a child is added or removed. | ||
useEffect(() => { | ||
node.current && | ||
mutationObserver.current.observe(node.current, { childList: true }); | ||
|
||
return () => mutationObserver.current.disconnect(); | ||
}, []); | ||
|
||
// Perform the animation when the `open` prop changes. | ||
useLayoutEffect(() => { | ||
openRef.current = open; | ||
if (!node.current) return; | ||
|
||
const wrapper = node.current; | ||
|
||
if (open) { | ||
wrapper.style.visibility = 'visible'; | ||
wrapper.style.height = `${initialHeight}px`; | ||
|
||
// after the animation has finished, remove the set height so the element can grow if it needs to. | ||
if (!open) { | ||
return timeoutEffect(() => { | ||
wrapper.style.overflowY = 'visible'; | ||
wrapper.style.height = 'initial'; | ||
}, speed); | ||
} else { | ||
// recalculate the height as it might have changed, then set it so it can be animated from. | ||
const newHeight = wrapper.scrollHeight; | ||
setInitialHeight(newHeight); | ||
wrapper.style.height = `${newHeight}px`; | ||
wrapper.style.overflowY = 'hidden'; | ||
|
||
return timeoutEffects( | ||
[ | ||
() => { | ||
wrapper.style.height = '0px'; | ||
}, | ||
0, | ||
], | ||
[ | ||
() => { | ||
wrapper.style.visibility = 'hidden'; | ||
}, | ||
speed, | ||
], | ||
); | ||
setMountChildren(false); | ||
}, ANIMATION_DURATION()); | ||
} | ||
|
||
setMountChildren(true); | ||
}, [open]); | ||
|
||
return ( | ||
<Wrapper ref={onRefConnect} duration={speed} className={className}> | ||
{children} | ||
</Wrapper> | ||
<GridCollapser open={open} className={className}> | ||
<CollapseInner>{mountChildren && children}</CollapseInner> | ||
</GridCollapser> | ||
); | ||
} | ||
|
||
interface WrapperProps { | ||
duration: number; | ||
interface GridCollapserProps { | ||
open?: boolean; | ||
} | ||
|
||
const Wrapper = styled.div<WrapperProps>` | ||
overflow-y: hidden; | ||
transition: height ${p => p.duration}ms ease-in-out; | ||
const GridCollapser = styled.div<GridCollapserProps>` | ||
display: grid; | ||
grid-template-rows: ${({ open }) => (open ? '1fr' : '0fr')}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so clean, love it |
||
transition: grid-template-rows ${() => ANIMATION_DURATION()}ms ease-in-out; | ||
@media (prefers-reduced-motion) { | ||
transition: unset; | ||
} | ||
`; | ||
|
||
const CollapseInner = styled.div` | ||
overflow: hidden; | ||
`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,12 +24,16 @@ export function Details({ | |
const [isOpen, setIsOpen] = React.useState(initialState); | ||
|
||
useEffect(() => { | ||
console.log('opening details'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. naughty |
||
setIsOpen(open); | ||
}, [open]); | ||
|
||
const toggleOpen = useCallback(() => { | ||
onStateToggle?.(!isOpen); | ||
setIsOpen(p => !p); | ||
setIsOpen(p => { | ||
onStateToggle?.(!p); | ||
|
||
return !p; | ||
}); | ||
}, []); | ||
|
||
return ( | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.