From e8b953ba6d5bb6f0df144c22fad0b1e4cba36094 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Mon, 29 Sep 2025 18:54:05 -0500 Subject: [PATCH] Add Disable And Enable A Button as a Tailwind TIL --- README.md | 3 +- tailwind/disable-and-enable-a-button.md | 72 +++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 tailwind/disable-and-enable-a-button.md diff --git a/README.md b/README.md index 81d11e7..656d311 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1656 TILs and counting..._ +_1657 TILs and counting..._ See some of the other learning resources I work on: - [Get Started with Vimium](https://egghead.io/courses/get-started-with-vimium~3t5f7) @@ -1470,6 +1470,7 @@ If you've learned something here, support my efforts writing daily TILs by - [Apply Tailwind Classes To Existing CSS Class](tailwind/apply-tailwind-classes-to-existing-css-class.md) - [Base Styles For Text Link](tailwind/base-styles-for-text-link.md) +- [Disable And Enable A Button](tailwind/disable-and-enable-a-button.md) - [Specify Paths For Purging Unused CSS](tailwind/specify-paths-for-purging-unused-css.md) - [Use Tailwind Typography Prose In Dark Mode](tailwind/use-tailwind-typography-prose-in-dark-mode.md) diff --git a/tailwind/disable-and-enable-a-button.md b/tailwind/disable-and-enable-a-button.md new file mode 100644 index 0000000..c51d6eb --- /dev/null +++ b/tailwind/disable-and-enable-a-button.md @@ -0,0 +1,72 @@ +# Disable And Enable A Button + +With TailwindCSS we can take a couple different approaches to tie the visual +and functional interactivity of a button to another elements state using +classes. + +One approach is to use +[`peer`](https://tailwindcss.com/docs/hover-focus-and-other-states#styling-based-on-sibling-state) +and `peer-checked:`. + +```html +
+ + + +
+``` + +Classes to make to button appear disabled (e.g. `opacity-40`) as well as +functional classes that affect interactivity (e.g. `pointer-events-none`) are +applied by default. When the sibling checkbox gets checked, the inverted +classes take effect making the button enabled. + +The `peer` approach works, but lacks flexibility. As soon as I need to make any +structural changes to the HTML that sever the peer (i.e. sibling) relationship +of the checkbox and the button, those classes stop working. + +With +[`group`](https://tailwindcss.com/docs/hover-focus-and-other-states#styling-based-on-the-descendants-of-a-group) +and `group-has-[:checked]:`, I can style the button relative to another +group member as long as everything is nested under some shared group tag. + +```html +
+
+ + +
+ +
+``` + +We can even utilize [named +groups](https://tailwindcss.com/docs/hover-focus-and-other-states#differentiating-nested-groups) +if we have overlapping and conflicting group interactions. But I won't get into +that here.