-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "scroll to top" button on events screen
- Loading branch information
1 parent
ab60a04
commit ef12c65
Showing
9 changed files
with
74 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,24 +1,64 @@ | ||
import React, { CSSProperties, ReactNode } from 'react' | ||
import React, { CSSProperties, FC, ReactNode, useCallback, useContext, useMemo, useState } from 'react' | ||
|
||
import useElementEvent from '../../hooks/useElementEvent' | ||
import { doNothing } from '../../utils' | ||
|
||
type Props<TView> = { | ||
views: readonly { readonly name: TView, readonly content: ReactNode }[], | ||
currentView: TView, | ||
} | ||
|
||
function MultiView<TView>({ views, currentView }: Props<TView>) { | ||
const [refs, setRefs] = useState<Array<HTMLElement | null>>(views.map(() => null)) | ||
const scrollHeights = useMemo(() => refs.map(ref => ref?.scrollTop ?? 0), [refs]) | ||
|
||
const setRef = (index: number) => (ref: HTMLElement | null) => { | ||
const newRefs = [...refs] | ||
newRefs[index] = ref | ||
setRefs(newRefs) | ||
} | ||
|
||
return ( | ||
<div className='multi-view' style={{ | ||
'--view-count': views.length, | ||
'--current-view': views.findIndex(e => e.name === currentView) | ||
} as CSSProperties}> | ||
<div className='sliding-container'> | ||
{views.map(({ name, content }) => | ||
<div className='slide' key={String(name)}> | ||
<Slide key={String(name)}> | ||
{content} | ||
</div>)} | ||
</Slide>)} | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
const Slide: FC<{ children: ReactNode }> = React.memo(({ children }) => { | ||
const [ref, setRef] = useState<HTMLElement | null>(null) | ||
const [scrollTop, setScrollTop] = useState(0) | ||
const [scrollHeight, setScrollHeight] = useState(0) | ||
const scrollToTop = useCallback(() => | ||
ref?.scrollTo({ top: 0, behavior: 'smooth' }) | ||
, [ref]) | ||
|
||
useElementEvent(ref, 'scroll', (_, ref) => setScrollTop(ref.scrollTop)) | ||
useElementEvent(ref, 'resize', (_, ref) => setScrollHeight(ref.scrollHeight)) | ||
|
||
const contextValue = useMemo(() => ({ scrollTop, scrollHeight, scrollToTop } as const), [scrollHeight, scrollToTop, scrollTop]) | ||
|
||
return ( | ||
<div className='slide' ref={setRef}> | ||
<SlideScrollContext.Provider value={contextValue}> | ||
{children} | ||
</SlideScrollContext.Provider> | ||
</div> | ||
) | ||
}) | ||
|
||
const SlideScrollContext = React.createContext({ scrollTop: 0, scrollHeight: 0, scrollToTop: doNothing }) | ||
|
||
export function useSlideScroll() { | ||
return useContext(SlideScrollContext) | ||
} | ||
|
||
export default React.memo(MultiView) as typeof MultiView |
This file contains 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { useEffect } from 'react' | ||
|
||
import { Maybe } from '../../../back-end/types/misc' | ||
|
||
export default function useElementEvent<E extends HTMLElement, K extends keyof HTMLElementEventMap>(ref: Maybe<E>, type: K, listener: (this: void, ev: HTMLElementEventMap[K], ref: E) => unknown) { | ||
useEffect(() => { | ||
if (ref) { | ||
const handle = (e: HTMLElementEventMap[K]) => listener(e, ref) | ||
ref.addEventListener(type, handle) | ||
return () => ref.removeEventListener(type, handle) | ||
} | ||
}, [listener, ref, type]) | ||
} |
This file contains 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
2 changes: 1 addition & 1 deletion
2
front-end/src/hooks/utils.ts → front-end/src/hooks/useWindowEvent.ts
This file contains 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 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 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,6 +1,6 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es5", | ||
"target": "es6", | ||
"lib": [ | ||
"dom", | ||
"dom.iterable", | ||
|