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

Button: prevent double-click & add loader #802

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

MikkelHansenAbtion
Copy link

Pet peeves of mine:

  • developers forgetting that <button /> onClick can be called twice by default
  • <button /> changing width/height when showing a loading text/component

This PR is intended to solve above points by:

  • handling onClick with isLoading state in the Button component
  • defaulting to a 'Loading...' text that is easy to change & displaying the necessary TW classes to center inside the button

Changed .eslintrc to allow console.log statements

Why: Nudge developers to consider using a loading
component & preventing users' double/multi-clicks
@MikkelHansenAbtion MikkelHansenAbtion added the wip Work in progress label Nov 11, 2024
@MikkelHansenAbtion
Copy link
Author

Realized we have 2 additional Button components where the difference between the 3 lies in the stylesheets only.

Debating with myself whether to have a base class (plenty of cons) vs merging the stylesheets in order to not repeat too much code...

Ideally the components are 100% self-contained when downloaded from the library, and we might actually just be better off with repeated logic. (No hook, extendable ButtonBase component or HOC)

@jeppester
Copy link
Contributor

jeppester commented Nov 12, 2024

I think this is a really useful addition. At the same time I am however not so happy that it adds internal state to our otherwise very simplistic button component.

I wonder if it would be a feasible solution to instead have a wrapper component that implements the loading functionality, e.g.:

<LoadWhileClicked loadingText="Loading...">
  <Button>Testing</Button>
</LoadWhileClicked>

I believe it should be possible to have LoadWhileClicked intercept the properties of Button as I've seen that implemented before (for instance by radix with asChild).

logic prevents double-click & shows "Loading..."
@MikkelHansenAbtion
Copy link
Author

Interesting idea, I will give it a shot later today💡

@MikkelHansenAbtion
Copy link
Author

The idea of a wrapper component that displays loading... text or a loading animation & hides existing content seems redundant to create if only paired with the Button, so I attempted to make it more generic. (ex to fit Dropdown, Select & Input)

It's proving nontrivial to me to ensure that content within a generic child component is not shown, while a custom loading text or icon replaces said content, positioned in the middle of the child element.
It's undoubtedly possible, but I feel like it's an overengineering rabbit hole to dive into and I think I should go no further.

I will leave this PR as is for now, unless we come up with other ideas.
To me, the current changes represent the ideal simple Button component to me and I respect that this is not a shared opinion ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wip Work in progress
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants