From ef9f88f3c82e35b835562253b988ab6ee9d35178 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Fri, 7 Mar 2025 21:30:52 -0600 Subject: [PATCH] Add Prevent Invisible Elements From Being Clicked as a CSS TIL --- README.md | 3 +- ...t-invisible-elements-from-being-clicked.md | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 css/prevent-invisible-elements-from-being-clicked.md diff --git a/README.md b/README.md index 535f203..7aa508e 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). -_1609 TILs and counting..._ +_1610 TILs and counting..._ See some of the other learning resources I work on: - [Ruby Operator Lookup](https://www.visualmode.dev/ruby-operators) @@ -191,6 +191,7 @@ If you've learned something here, support my efforts writing daily TILs by - [Lighten And Darken With SCSS](css/lighten-and-darken-with-scss.md) - [Make A Block Of Text Respect New Lines](css/make-a-block-of-text-respect-new-lines.md) - [Parameterized SCSS Mixins](css/parameterized-scss-mixins.md) +- [Prevent Invisible Elements From Being Clicked](css/prevent-invisible-elements-from-being-clicked.md) - [:root Has Higher Specificity Than html](css/root-has-higher-specificity-than-html.md) - [Style A Background With A Linear Gradient](css/style-a-background-with-a-linear-gradient.md) - [Using Maps In SCSS](css/using-maps-in-scss.md) diff --git a/css/prevent-invisible-elements-from-being-clicked.md b/css/prevent-invisible-elements-from-being-clicked.md new file mode 100644 index 0000000..d510003 --- /dev/null +++ b/css/prevent-invisible-elements-from-being-clicked.md @@ -0,0 +1,29 @@ +# Prevent Invisible Elements From Being Clicked + +I have a nav element that when clicked reveals a custom drop-down menu. It +reveals it using CSS transitions and transformations (`opacity` and `scale`). +When the nav element is clicked again, the reverse of these transformations is +applied to "hide" the menu. This gives a nice visual effect. + +It only makes the menu invisible and doesn't actually make it go away. That +means that menu could be invisible, but hovering over the top of a button on +the screen. The button cannot be clicked now because the menu is intercepting +that [_pointer +event_](https://developer.mozilla.org/en-US/docs/Web/CSS/pointer-events). + +The fix is to apply CSS (or a class) when the drop-down menu is closed that +tells it to ignore _pointer events_. + +```css +.pointer-events-none { + pointer-events: none; +} +``` + +This is more of less what [the `pointer-events-none` TailwindCSS +utility](https://tailwindcss.com/docs/pointer-events) looks like. + +This class is applied by default to the drop-down menu. Then when the nav item +is clicked, some JavaScript removes that class at the same moment that the menu +is visually appearing. When a menu item is selected or the menu otherwise +closed, it transitions away and the `pointer-events-none` class is reapplied.