Skip to content

Commit

Permalink
add BackToTop button (#620)
Browse files Browse the repository at this point in the history
  • Loading branch information
robphoenix authored Nov 25, 2024
1 parent 51768bc commit 3cd13be
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/web-ui/src/components/Alert/Alert.docs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Meta, Canvas, ArgTypes } from '@storybook/blocks';

import * as Stories from './Alert.stories';

import { DocsHeader } from '../../storybook-components';
import { DocsHeader, BackToTop } from '../../storybook-components';

<Meta of={Stories} />

Expand Down
47 changes: 47 additions & 0 deletions packages/web-ui/src/storybook-components/BackToTop.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as React from 'react';

import { Unstyled } from '@storybook/blocks';

Check failure on line 3 in packages/web-ui/src/storybook-components/BackToTop.tsx

View workflow job for this annotation

GitHub Actions / Code Checks

'Unstyled' is defined but never used

import { ChevronUpSmallIcon } from '@utilitywarehouse/react-icons';

import { Button } from '../components';

export const BackToTop = () => {
const [visible, setVisible] = React.useState(false);

const toggleVisible = () => {
const scrolled = document.documentElement.scrollTop;
if (scrolled > 300) {
setVisible(true);
} else if (scrolled <= 300) {
setVisible(false);
}
};

const scrollToTop = () => {
window.scrollTo({
top: 0,
behavior: 'smooth',
});
};

window.addEventListener('scroll', toggleVisible);

return (
<Button
size="small"
onClick={scrollToTop}
variant="ghost"
sx={{
display: visible ? 'flex' : 'none',
position: 'fixed',
right: 20,
bottom: 40,
zIndex: 1,
}}
>
<ChevronUpSmallIcon />
Back to top
</Button>
);
};
3 changes: 3 additions & 0 deletions packages/web-ui/src/storybook-components/DocsHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import * as React from 'react';

import { Unstyled, Description } from '@storybook/blocks';

import { BackToTop } from './BackToTop';

import { Alert, Flex, Heading, Link } from '../components';

interface DocsHeaderProps {
Expand Down Expand Up @@ -39,5 +41,6 @@ export const DocsHeader = ({
/>
) : null}
</Flex>
<BackToTop />
</Unstyled>
);
1 change: 1 addition & 0 deletions packages/web-ui/src/storybook-components/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './Backgrounds';
export * from './DocsHeader';
export * from './Placeholder';
export * from './BackToTop';

0 comments on commit 3cd13be

Please sign in to comment.