-
Notifications
You must be signed in to change notification settings - Fork 21
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
feat(notifications): use chakra notifications #384
Merged
Merged
Changes from 12 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
bf74ef3
notifier, basic story
snqb 4bd05b2
fix dark mode
snqb b56aedd
autodocs
snqb ccb76f4
write tests
snqb b789af3
a bit of cleanup
snqb cdcfd9a
cleanup 2
snqb 175cf3e
cosmetics
snqb 66938e1
fix dark mode, misc
snqb 6f0199e
cleanup
snqb a253f10
prevent hovered notification from hiding
snqb 9faef41
duration 1e6 sounds too big, 1e3
snqb 01d2a44
add support for info and warning
snqb 83ce066
add explanatory comment about eslintrc
snqb 400172c
cleanup preview.jsx
snqb 572bbf2
add standalone variants, add tests
snqb 9a744a6
fix lint
snqb 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 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,25 +1,46 @@ | ||
import { ColorModeScript } from '@chakra-ui/react'; | ||
import { DocsContainer } from '@storybook/addon-docs'; | ||
import { themes } from '@storybook/theming'; | ||
import React from 'react'; | ||
import { useDarkMode } from 'storybook-dark-mode'; | ||
import { ChakraThemeProvider } from '../src/chakra'; | ||
|
||
import ThemeProvider from '../src/theme/ThemeProvider'; | ||
|
||
export const parameters = { | ||
actions: { argTypesRegex: '^on[A-Z].*' }, | ||
layout: 'centered', | ||
controls: { expanded: true }, | ||
chakra: {}, | ||
docs: { | ||
container: (props) => { | ||
const isDark = useDarkMode(); | ||
|
||
return ( | ||
<DocsContainer {...props} theme={isDark ? themes.dark : themes.light} /> | ||
); | ||
}, | ||
}, | ||
darkMode: { | ||
current: 'light', | ||
dark: { ...themes.dark, appBg: themes.dark.appBg }, | ||
light: { ...themes.light, appBg: themes.light.appBg }, | ||
dark: { ...themes.dark }, | ||
light: { ...themes.light }, | ||
stylePreview: true, | ||
}, | ||
}; | ||
|
||
export const decorators = [ | ||
(Story) => ( | ||
<ThemeProvider colorMode={useDarkMode() ? 'dark' : 'light'}> | ||
<Story /> | ||
</ThemeProvider> | ||
), | ||
(Story) => { | ||
const isDark = useDarkMode(); | ||
|
||
return ( | ||
<> | ||
<ColorModeScript /> | ||
|
||
<ChakraThemeProvider> | ||
<ThemeProvider colorMode={isDark ? 'dark' : 'light'}> | ||
<Story /> | ||
</ThemeProvider> | ||
</ChakraThemeProvider> | ||
</> | ||
); | ||
}, | ||
]; |
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,3 +1,5 @@ | ||
export { useDisclosure } from '@chakra-ui/react'; | ||
export { useDisclosure, ColorModeScript } from '@chakra-ui/react'; | ||
|
||
export { ChakraThemeProvider } from './theme-chakra/ChakraThemeProvider'; | ||
|
||
export * from './components/notifier'; |
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { | ||
Alert, | ||
AlertDescription, | ||
AlertProps, | ||
AlertTitle, | ||
CloseButton, | ||
} from '@chakra-ui/react'; | ||
import React, { ReactNode } from 'react'; | ||
import { Flex } from '../flex'; | ||
|
||
interface Props extends Omit<AlertProps, 'title'> { | ||
onClose: any; | ||
title: string | ReactNode; | ||
content: string | ReactNode; | ||
} | ||
|
||
export const Notification = ({ | ||
onClose, | ||
content, | ||
title, | ||
status, | ||
...restProps | ||
}: Props) => { | ||
return ( | ||
<Alert | ||
variant="left-accent" | ||
status={status} | ||
borderLeftWidth="5px" | ||
flexDirection="column" | ||
alignItems="start" | ||
minW="350px" | ||
bg="white" | ||
_dark={{ | ||
bg: 'dark.white', | ||
}} | ||
gap={1} | ||
pb={4} | ||
pl={6} | ||
{...restProps} | ||
> | ||
<Flex width="100%" justifyContent="space-between" alignItems="flex-start"> | ||
{React.isValidElement(title) ? ( | ||
title | ||
) : ( | ||
<AlertTitle fontSize="md">{title}</AlertTitle> | ||
)} | ||
<CloseButton onClick={onClose} /> | ||
</Flex> | ||
|
||
{React.isValidElement(content) ? ( | ||
content | ||
) : ( | ||
<AlertDescription fontSize="xs">{content}</AlertDescription> | ||
)} | ||
</Alert> | ||
); | ||
}; | ||
|
||
export interface INotification { | ||
/** Title of the alert. */ | ||
title: string | ReactNode; | ||
/** Content under the title. */ | ||
content: string | ReactNode; | ||
/** Duration in milliseconds. E.g. 5000 by default */ | ||
duration?: number; | ||
isError?: boolean; | ||
} |
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 @@ | ||
export { useNotifier, createNotifier } from './notifier'; |
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,87 @@ | ||
import { AlertStatus, ToastId, useToast } from '@chakra-ui/react'; | ||
import React, { ReactNode, useCallback } from 'react'; | ||
import { standaloneToast } from '../../theme-chakra/ChakraThemeProvider'; | ||
import { Notification } from './Notification'; | ||
|
||
export interface INotification { | ||
title: string | ReactNode; | ||
content: string | ReactNode; | ||
duration?: number; | ||
status?: AlertStatus; | ||
} | ||
|
||
export const useNotifier = () => { | ||
const toast = useToast(); | ||
|
||
const notify = useCallback( | ||
(status: AlertStatus) => (notification: INotification) => { | ||
const render = ({ onClose, id }: { onClose(): void; id: ToastId }) => { | ||
return ( | ||
<Notification | ||
title={notification.title} | ||
content={notification.content} | ||
onClose={onClose} | ||
status={status} | ||
onMouseEnter={() => hoverHandler(id)} | ||
onMouseLeave={() => unhoverHandler(id)} | ||
/> | ||
); | ||
}; | ||
|
||
const hoverHandler = (id: ToastId) => { | ||
toast.update(id, { duration: 1e6, render }); | ||
}; | ||
|
||
const unhoverHandler = (id: ToastId) => { | ||
toast.update(id, { | ||
duration: notification.duration ?? 5000, | ||
render, | ||
}); | ||
}; | ||
|
||
toast({ | ||
status, | ||
duration: notification.duration ?? 5000, | ||
isClosable: true, | ||
render, | ||
}); | ||
}, | ||
[toast], | ||
); | ||
|
||
return { | ||
success: notify('success'), | ||
error: notify('error'), | ||
info: notify('info'), | ||
warning: notify('warning'), | ||
closeAll: toast.closeAll, | ||
close: toast.close, | ||
}; | ||
}; | ||
|
||
export const createNotifier = () => { | ||
const notify = (status: AlertStatus) => (notification: INotification) => | ||
standaloneToast({ | ||
status, | ||
duration: notification.duration ?? 5000, | ||
isClosable: true, | ||
position: 'top-right', | ||
render: ({ onClose }) => { | ||
return ( | ||
<Notification | ||
title={notification.title} | ||
content={notification.content} | ||
onClose={onClose} | ||
status={status} | ||
/> | ||
); | ||
}, | ||
}); | ||
|
||
return { | ||
snqb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
success: notify('success'), | ||
error: notify('error'), | ||
closeAll: standaloneToast.closeAll, | ||
close: standaloneToast.close, | ||
}; | ||
}; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it added no value and rendered a second dark mode button which didn't work fine, because we have chakra+rebass :(