diff --git a/packages/code-studio/src/styleguide/GotoTopButton.css b/packages/code-studio/src/styleguide/GotoTopButton.css new file mode 100644 index 0000000000..681a8f8871 --- /dev/null +++ b/packages/code-studio/src/styleguide/GotoTopButton.css @@ -0,0 +1,19 @@ +/* + * GotoTopButton is only visible if user has scrolled down. Visibility attribute + * can't really make use of CSS transitions, so we use opacity instead. Including + * visibility for accessibility reasons. + */ +.goto-top-button { + visibility: visible; + opacity: 1; + transition: + opacity 300ms, + visibility 0s linear 0s; +} +html:not([data-scroll='true']) .goto-top-button { + visibility: hidden; + opacity: 0; + transition: + opacity 300ms, + visibility 0s linear 300ms; +} diff --git a/packages/code-studio/src/styleguide/GotoTopButton.tsx b/packages/code-studio/src/styleguide/GotoTopButton.tsx index 361e908b86..f51039c4f7 100644 --- a/packages/code-studio/src/styleguide/GotoTopButton.tsx +++ b/packages/code-studio/src/styleguide/GotoTopButton.tsx @@ -1,10 +1,11 @@ -import React, { useCallback } from 'react'; +import React, { useCallback, useEffect } from 'react'; import { Button, Icon } from '@adobe/react-spectrum'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { vsChevronUp } from '@deephaven/icons'; +import './GotoTopButton.css'; /** - * Button that scrolls to top of page and clears location hash. + * Button that scrolls to top of styleguide and clears location hash. */ export function GotoTopButton(): JSX.Element { const gotoTop = useCallback(() => { @@ -19,8 +20,24 @@ export function GotoTopButton(): JSX.Element { }, 500); }, []); + // Set data-scroll="true" on the html element when the user scrolls down below + // 120px. CSS uses this to only show the button when the user has scrolled. + useEffect(() => { + function onScroll() { + document.documentElement.dataset.scroll = String(window.scrollY > 120); + } + document.addEventListener('scroll', onScroll, { passive: true }); + return () => { + document.removeEventListener('scroll', onScroll); + }; + }, []); + return ( -