-
Notifications
You must be signed in to change notification settings - Fork 64
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
Typescript migration of snackbar stories #808
Changes from all commits
36b49bd
c359bcd
49b49fd
169ecdf
0f002dd
6c12906
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import React, { useState } from 'react' | ||
import { Snackbar, SnackbarProps, Button } from '@equinor/eds-core-react' | ||
import { Story, Meta } from '@storybook/react' | ||
|
||
const { SnackbarAction } = Snackbar | ||
|
||
export default { | ||
title: 'Components/Snackbar', | ||
component: Snackbar, | ||
subcomponents: { SnackbarAction }, | ||
} as Meta | ||
|
||
export const Default: Story<SnackbarProps> = (args) => { | ||
const { open } = args | ||
const [visible, setVisible] = useState(open) | ||
return ( | ||
<> | ||
<Button type="button" onClick={() => setVisible(true)}> | ||
Show a simple snackbar with default options | ||
</Button> | ||
<Snackbar {...args} open={visible} onClose={() => setVisible(false)}> | ||
Play with me | ||
</Snackbar> | ||
</> | ||
) | ||
} | ||
export const Simple: Story<SnackbarProps> = () => { | ||
const [open, setOpen] = useState(false) | ||
|
||
return ( | ||
<> | ||
<Button type="button" onClick={() => setOpen(true)}> | ||
Show a simple snackbar for 5 seconds | ||
</Button> | ||
<Snackbar | ||
open={open} | ||
onClose={() => setOpen(false)} | ||
autoHideDuration={5000} | ||
leftAlignFrom="1500px" | ||
> | ||
Message goes here | ||
</Snackbar> | ||
</> | ||
) | ||
} | ||
|
||
export const WithAction: Story<SnackbarProps> = () => { | ||
const [withActionOpen, setWithActionOpen] = useState(false) | ||
return ( | ||
<> | ||
<Button type="button" onClick={() => setWithActionOpen(true)}> | ||
Show a snackbar with action for the default 7 seconds | ||
</Button> | ||
<Snackbar open={withActionOpen} onClose={() => setWithActionOpen(false)}> | ||
Your changes was saved | ||
<SnackbarAction> | ||
<Button variant="ghost">Undo</Button> | ||
</SnackbarAction> | ||
</Snackbar> | ||
</> | ||
) | ||
} | ||
|
||
WithAction.storyName = 'With action' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import React, { useState, useEffect, HTMLAttributes } from 'react' | ||
import React, { useState, useEffect, HTMLAttributes, FC } from 'react' | ||
import styled from 'styled-components' | ||
import { snackbar as tokens } from './Snackbar.tokens' | ||
import { typographyTemplate } from '../_common/templates' | ||
|
@@ -23,6 +23,7 @@ const StyledSnackbar = styled.div.attrs(() => ({ | |
box-sizing: border-box; | ||
left: 50%; | ||
transform: translateX(-50%); | ||
z-index: 10; | ||
@media (min-width: ${({ leftAlignFrom }) => leftAlignFrom}) { | ||
left: auto; | ||
transform: none; | ||
|
@@ -34,25 +35,25 @@ const StyledSnackbar = styled.div.attrs(() => ({ | |
} | ||
` | ||
|
||
type Props = { | ||
/* Controls the visibility of the snackbar */ | ||
export type SnackbarProps = { | ||
/** Controls the visibility of the snackbar */ | ||
open?: boolean | ||
/* How long will the message be visible in milliseconds */ | ||
/** How long will the message be visible in milliseconds */ | ||
autoHideDuration?: number | ||
/* Callback fired when the snackbar is closed by auto hide duration timeout */ | ||
/** Callback fired when the snackbar is closed by auto hide duration timeout */ | ||
onClose?: () => void | ||
/* Media query from which the snackbar will be horizontal centered */ | ||
/** Media query from which the snackbar will be horizontal centered */ | ||
leftAlignFrom?: string | ||
} & HTMLAttributes<HTMLDivElement> | ||
|
||
export const Snackbar = ({ | ||
export const Snackbar: FC<SnackbarProps> = ({ | ||
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. @wenche Why 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. See commit message. It didn't have forwardRef and adding it will be a breaking change. 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. Oh.. So we should add |
||
open = false, | ||
autoHideDuration = 7000, | ||
onClose, | ||
leftAlignFrom = '1200px', | ||
children, | ||
className, | ||
}: Props): JSX.Element => { | ||
}) => { | ||
const [visible, setVisible] = useState(open) | ||
useEffect(() => { | ||
setVisible(open) | ||
|
@@ -75,4 +76,4 @@ export const Snackbar = ({ | |
) | ||
} | ||
|
||
// Snackbar.displayName = 'eds-snackbar' | ||
// Snackbar.displayName = 'Snackbar' |
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.
You can remove
const { open } = args
here and let the user trigger only at button click, that way the user can play with the otherSnackbar
examples too without interference... But it's weird how we get errors on it because this method has worked before 😕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.
That is the my main issue - how do we know that we are not just masking errors by just removing code that doesn't work? And then I'll have to turn off the control for open as well 😬
For the record, I'm doing the same thing with the Progress indicators - but because they is a single component with a single type it just works. But obviously not with the composed component approach
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.
Open
is right there!🤔
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.
I also did it on Popover with subcomponents