Skip to content
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

Merged
merged 14 commits into from
Jul 8, 2021

Conversation

josh-luong
Copy link
Contributor

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 prop isOpen when it was used as a controlled component. As a result, this would lead to some undesired behaviour such as canAutoClose breaking the controlled toast.

  • Updated behaviour such that the internal state isToastOpen solely handles the render flow of the toast, and is toggled by isOpen 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

@changeset-bot
Copy link

changeset-bot bot commented Jun 10, 2021

🦋 Changeset detected

Latest commit: 56cab33

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 5 packages
Name Type
@paprika/toast Patch
@paprika/copy-input Patch
@paprika/data-header Patch
@paprika/list-box Patch
@paprika/inline-editors Patch

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

@@ -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). |
Copy link
Contributor

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?

Copy link
Contributor

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).

Comment on lines 16 to 17
<Toast hasCloseButton>Uncontrolled toast component</Toast>
<Toast canAutoClose autoCloseDelay={5000} hasCloseButton>
Copy link
Contributor

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>
Copy link
Contributor

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}>
Copy link
Contributor

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>

@@ -48,23 +48,34 @@ const Toast = React.forwardRef((props, ref) => {
},
}));

const [isToastOpen, setIsToastOpen] = React.useState(isOpen === undefined ? true : isOpen);
const isControlled = isOpen !== undefined;
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting!

@nahumzs
Copy link
Contributor

nahumzs commented Jul 8, 2021

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.

@leonatgalvanize leonatgalvanize merged commit 9e9c692 into master Jul 8, 2021
@leonatgalvanize leonatgalvanize deleted the UXD-1237-refactor-toast-component branch July 8, 2021 20:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants