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

Add controlled prop to open the Tooltip #11714

Merged
merged 8 commits into from
Mar 14, 2024
Merged

Conversation

aaronccasanova
Copy link
Member

@aaronccasanova aaronccasanova commented Mar 11, 2024

WHAT is this pull request doing?

This PR introduces a controlled component prop named open to the Tooltip. This feature is being added to support upcoming Common Actions guidance for Copy to Clipboard buttons.

Currently, the Tooltip provides an active prop which only sets the initial open state. Note: The new open prop takes precedence over active. I suggest we rename active to defaultOpen in a future major release for consistency with other (un)controlled component patterns (e.g. <input />, value, and defaultValue) and help disambiguate the props.

UPDATE: Per the discussion here, we have opt'd to include the proposed defaultProp and deprecate the active prop as part of this PR e.g.

  • Added a new defaultOpen prop
  • Deprecated the active prop
  • Special cased the existing active prop behavior
  • Added new test cases for defaultOpen
  • Updated existing test cases to use defaultOpen
  • Added uncontrolled Tooltip examples to Storybook
  • Updated existing Storybook entries to use defaultOpen

Example setting open={true} for the duration of the copied status timeout:

Screen.Recording.2024-03-01.at.3.11.05.PM.mov

How to 🎩

🖥 Local development instructions
🗒 General tophatting guidelines
📄 Changelog guidelines

🎩 checklist

.changeset/three-plants-smile.md Outdated Show resolved Hide resolved
polaris-react/src/components/Tooltip/Tooltip.stories.tsx Outdated Show resolved Hide resolved
/** Toggle whether the tooltip is visible */
/** Toggle whether the tooltip is visible. Takes precedence over `active` */
open?: boolean;
/** Toggle whether the tooltip is visible initially */
active?: boolean;
Copy link
Member

Choose a reason for hiding this comment

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

Should we deprecate active and add the defaultOpen prop now in anticipation of v13?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea 👍 I'll push up a commit shortly.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, I tentatively think we should handle this in a follow up. I attempted the rename here. However, the active props seems to have one controlled prop-like behavior in that active={false} does force the closed state... We can special case the existing active behavior with conditionals for backward compatibility or just wait till Polaris v13. Thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

Even though defaultOpen doesn't behave exactly like active, I think it is worth deprecating active here and adding defaultOpen. I think active is currently an odd prop mix of part-controlled uncontrolled.

I think it would be better to leave the active behavior as-is until and remove it on the next major.

Alternatively, we introduce a defaultActive prop and "fix" active to behave like a fully controlled prop (state is external). Though, this strategy would also be a breaking change which would delay Common Action progress

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it would be better to leave the active behavior as-is until and remove it on the next major.

Agreed 👍

Copy link
Member Author

@aaronccasanova aaronccasanova Mar 12, 2024

Choose a reason for hiding this comment

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

Update:

  • Added a new defaultOpen prop
  • Deprecated the active prop
  • Special cased the existing active prop behavior
  • Added new test cases for defaultOpen
  • Updated existing test cases to use defaultOpen
  • Added uncontrolled Tooltip examples to Storybook
  • Updated existing Storybook entries to use defaultOpen

Copy link
Member

Choose a reason for hiding this comment

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

Without adding onEnter/onLeave handlers for consumers to control active state within Tooltip itself, how does open get controlled? I see that it's controlled externally by some other interactive activator element in the story, but the tooltip's presence being controlled by another element doesn't make sense 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

That's actually my current use case in the Copy to Clipboard recipe (e.g. the copy button triggers a timeout which sets open={true} only). However, I agree with the sentiment it is more conventional to show controlled component examples with the state and handlers 👍 Updated! 7619a24

/** Toggle whether the tooltip is visible */
/** Toggle whether the tooltip is visible. Takes precedence over `active` */
open?: boolean;
/** Toggle whether the tooltip is visible initially */
active?: boolean;
Copy link
Member

Choose a reason for hiding this comment

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

Even though defaultOpen doesn't behave exactly like active, I think it is worth deprecating active here and adding defaultOpen. I think active is currently an odd prop mix of part-controlled uncontrolled.

I think it would be better to leave the active behavior as-is until and remove it on the next major.

Alternatively, we introduce a defaultActive prop and "fix" active to behave like a fully controlled prop (state is external). Though, this strategy would also be a breaking change which would delay Common Action progress

@aaronccasanova
Copy link
Member Author

cc @chloerice @laurkim @sam-b-rose requested new reviews as a LOT has changed per the discussion here

Copy link
Member

@chloerice chloerice left a comment

Choose a reason for hiding this comment

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

Works great @aaronccasanova, can't wait to be rid of that active prop 🙌🏽 🚀

@aaronccasanova
Copy link
Member Author

/snapit

Copy link
Contributor

🫰✨ Thanks @aaronccasanova! Your snapshots have been published to npm.

Test the snapshots by updating your package.json with the newly published versions:

yarn add @shopify/[email protected]
yarn add @shopify/[email protected]

@aaronccasanova aaronccasanova merged commit 2f0cbca into main Mar 14, 2024
12 checks passed
@aaronccasanova aaronccasanova deleted the tooltip-controlled-prop branch March 14, 2024 20:11
aaronccasanova added a commit that referenced this pull request Mar 16, 2024
aaronccasanova added a commit that referenced this pull request Mar 16, 2024
Reverts #11714

@chloerice and I are exploring an alternative pattern for hybrid
controlled/uncontrolled component state management.
This pull request was closed.
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.

4 participants