-
Notifications
You must be signed in to change notification settings - Fork 9
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
fix(Toast): UXD-1237 Refactor toast internal states and fix canAutoClose for controlled #1079
Conversation
…led and uncontrolled
…rvices/paprika into UXD-1177-refactor-toast-component
🦋 Changeset detectedLatest commit: 56cab33 The changes in this PR will be included in the next version bump. This PR includes changesets to release 5 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
``` | ||
|
||
Note: An uncontrolled Toast is expected to be displayed and opened once, if the desired behavior is to display the Toast more than once, an alternative method is to reset the Toast by updating its key or use a controlled Toast component instead. | ||
|
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.
💯
packages/Toast/README.md
Outdated
@@ -25,10 +25,10 @@ npm install @paprika/toast | |||
| Prop | Type | required | default | Description | | |||
| -------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | --------------------- | ----------------------------------------------------------------------------------------------------------------------------------- | | |||
| autoCloseDelay | number | false | 5000 | Duration (in ms) before Toast will automatically close (if canAutoClose is true). | | |||
| canAutoClose | bool | false | false | Will automatically close after 1500ms (or longer if provided by autoCloseDelay). | | |||
| canAutoClose | bool | false | false | Will automatically close after 5000ms (or longer if provided by autoCloseDelay). | |
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.
is 5000ms a new value? was this suggested by a ux-design :) how did we arrive at this value?
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.
The default value for autoCloseDelay was always 5000ms, so technically the default behaviour was that, so I updated the docs to reflect that. However, there is a 'minimumCloseTime' of 1500ms.
Right now the timeout timer is set by Math.max(autoCloseDelay, minimumCloseTime).
<Toast hasCloseButton>Uncontrolled toast component</Toast> | ||
<Toast canAutoClose autoCloseDelay={5000} hasCloseButton> |
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.
A little out of scope, but while we're here, I think it makes sense to clean up this story just a little. There are some default props added here that are not actually needed, which is a distraction from the use case being illustrated.
return ( | ||
<L10n locale="en"> | ||
<Toast hasCloseButton>Uncontrolled toast component</Toast> | ||
<Toast canAutoClose autoCloseDelay={5000} hasCloseButton> | ||
Uncontrolled toast component with auto close | ||
</Toast> | ||
<CodeViewer> |
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.
While I appreciate the creative use of the <CodeViewer>
here, I think maybe it would be more useful to just link to the whole story code on GitHub – a pattern established in several other components stories (<Input>
is one example you can reference). Essentially by using the <ExampleStory>
helper we can include a source
link in the header.
<Toast key={accumulator} canAutoClose autoCloseDelay={5000}> | ||
Uncontrolled toast with auto close and ability to re-open upon button click | ||
</Toast> | ||
<button type="button" onClick={handleItem}> |
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.
Minor suggestion: the story would be a little nicer looking with a Paprika <Button>
packages/Toast/src/Toast.js
Outdated
@@ -48,23 +48,34 @@ const Toast = React.forwardRef((props, ref) => { | |||
}, | |||
})); | |||
|
|||
const [isToastOpen, setIsToastOpen] = React.useState(isOpen === undefined ? true : isOpen); | |||
const isControlled = isOpen !== undefined; |
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.
typeof isOpen === "undefined" is a better approach readmore
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.
interesting!
Lets merge this one if you think is at enough good stage @josh-luong, I think this branch has been running for more than a month, and the work is good enough for this iteration. As long as test are passing I'm 👌 with all this work. |
Purpose 🚀
Refactor how the internal state is coupled when using the Toast as a controlled or uncontrolled component.
Notes ✏️
In the current toast, the internal states
isToastOpen
,shouldRender
was overcomplicated and coupled with the parent propisOpen
when it was used as a controlled component. As a result, this would lead to some undesired behaviour such ascanAutoClose
breaking the controlled toast.Updated behaviour such that the internal state
isToastOpen
solely handles the render flow of the toast, and is toggled byisOpen
when controlled.Updated documentation and added a story to exemplify when the consumer wants an uncontrolled toast that can be rerendered by updating the component's key to reset its internal state.
Storybook 📕
http://storybooks.highbond-s3.com/paprika/UXD-1177-refactor-toast-component
References 🔗
relevant Jira ticket / GitHub issues
https://aclgrc.atlassian.net/browse/UXD-1177
https://aclgrc.atlassian.net/browse/UXD-1136
#1016
Coauthor: @josh-luong @leonatgalvanize