Detect Clicks Anywhere on a Page in React

  Рет қаралды 3,546

Colby Fayock

Colby Fayock

Күн бұрын

Пікірлер: 22
@colbyfayock
@colbyfayock 3 ай бұрын
Learn how to build a full stack Next.js app in my upcoming course: colbyfayock.com/course
@npf21
@npf21 9 ай бұрын
Loved how you broke it down solving the issue in simple terms. Its definitely one that could be made into a readily available hook as it could be used in multiple dialogs (search bar, drop down filters, ...). In one of my projects we just had toggleDialog function to switch state but not to include escape in MUI but i think it has onClose hook that you can handle outside click or escape. What would be a specific example edge case where you would use input type as text over search?
@colbyfayock
@colbyfayock 9 ай бұрын
definitely agree, would make a nice hook! id mostly use the type="search" for search-related inputs. just off of the top of my head, if you were doing an address autocomplete input where the default experience would be a standard address form, but if you start typing into the Address 1 input, you can provide a little dropdown to fill out the rest based off an address API. although that still feels like a "search", given the default experience is not a search, rather the search is a helper/enhancement, you'd likely semantically want to use an input type of text similarly if maybe you're doing an email input where you autocomplete some common email platforms, such as ['gmail.com','yahoo.com',etc]. you'd want to use a type of email there, but provide the ability for someone to dismiss the UI a standard search input like this example is probably the most common pattern though
@John-eq5cd
@John-eq5cd 6 ай бұрын
I came to this from Colby's video on the dialog HTML element with the idea of applying the click-close code to a pop-up dialog. However, I just couldn't get it to work when checking if event.composedPath().includes the ref for the dialog element. Surprisingly, the dialog element, when open, seemed to take over the whole of the Section element, wherever I clicked it was still on the dialog and so it would not close. Therefore I created a variable for the image within the dialog using getElementById and when clicking away from this the dialog successfully closed.
@colbyfayock
@colbyfayock 6 ай бұрын
hey! what i ended up doing in the dialog example was applying the ref to the inner div which in my case was expanded to the size of the inner dialog. glad you got something working though!
@hassanad94
@hassanad94 9 ай бұрын
Nice Thought... This kind of mentality opened up a lot of things for me... I started my carrier with vanilla js, and after a 2-3 years i learned react. Since than i have some struggles... How to do things the react way... But this solution is clear... :D Thank you :D Just buy seeing this i get to do lot of other things :D
@colbyfayock
@colbyfayock 9 ай бұрын
glad you enjoyed it! it's definitely important to find ways of being able to achieve things like this without "breaking out' of the "react way" as much as you can, but luckily this doesnt interfere with React's internal operations to make it a great solution
@erikslorenz
@erikslorenz 9 ай бұрын
The new dialog element is the GOAT. This is one of those common patterns that is really annoying when using a client side framework.
@colbyfayock
@colbyfayock 9 ай бұрын
I definitely need to spend some more time with it
@erikslorenz
@erikslorenz 9 ай бұрын
between dialog and popover api that's like a whole category of stuff that makes my life easier. I actually think its also more difficult to use in a frontend framework vs just vanilla js since you need to access the elements directly. Feels like those "you probably dont need jquery" years are coming full circle but this time with react and such lol@@colbyfayock
@bandekhoda7801
@bandekhoda7801 9 ай бұрын
I was actually wondering how to replicate this type of a behaviour, and boom...
@colbyfayock
@colbyfayock 9 ай бұрын
💥 glad it helped!
@CanRau
@CanRau 9 ай бұрын
Basically only every update interactively, started doing this with yarn v1, can’t go back to non interactive
@colbyfayock
@colbyfayock 9 ай бұрын
hey not totally sure what you're referring to?
@CanRau
@CanRau 9 ай бұрын
This was meant for another video but while I posted my watch list has already moved to the next video, hoped YT would properly handle this but apparently doesn't 😅
@colbyfayock
@colbyfayock 9 ай бұрын
Get fresh tutorials and other free content straight to your inbox! colbyfayock.com/news
@parubok
@parubok 5 ай бұрын
Thanks!
@projectnum0593
@projectnum0593 5 ай бұрын
Thanks
@colbyfayock
@colbyfayock 5 ай бұрын
youre welcome!
@regedieddy
@regedieddy 5 ай бұрын
Thanks man. Your solution works fine. Before i did try to use !ref.current.contains(event.target)) but it didn't work for me. Contains method did not find node.
@colbyfayock
@colbyfayock 5 ай бұрын
ah, glad this worked for you!
Get Selected Text in React
25:12
Colby Fayock
Рет қаралды 2,7 М.
Favicons the Right Way
18:07
Colby Fayock
Рет қаралды 11 М.
Ice Cream or Surprise Trip Around the World?
00:31
Hungry FAM
Рет қаралды 22 МЛН
Симбу закрыли дома?! 🔒 #симба #симбочка #арти
00:41
Симбочка Пимпочка
Рет қаралды 4,9 МЛН
When u fight over the armrest
00:41
Adam W
Рет қаралды 32 МЛН
Noodles Eating Challenge, So Magical! So Much Fun#Funnyfamily #Partygames #Funny
00:33
Build your own Notion-like WYSIWYG Editor in React
20:27
Colby Fayock
Рет қаралды 17 М.
Click Outside to Close - React Hook
9:15
Ryan Toronto
Рет қаралды 51 М.
Styling active links in next.js is tricker than you think
10:02
Colby Fayock
Рет қаралды 3,8 М.
Add to Calendar Button with React & Tailwind
15:06
Colby Fayock
Рет қаралды 2,6 М.
Generate a PDF in React
23:38
Colby Fayock
Рет қаралды 24 М.
Best Way to Add Popup Modals in React
26:19
Colby Fayock
Рет қаралды 10 М.
This Fixes CORS
13:02
Colby Fayock
Рет қаралды 6 М.
You Don't Need a CMS - Use This Instead
28:55
Colby Fayock
Рет қаралды 26 М.
Why Agent Frameworks Will Fail (and what to use instead)
19:21
Dave Ebbelaar
Рет қаралды 91 М.
Pagination in React
29:05
Colby Fayock
Рет қаралды 3,8 М.
Ice Cream or Surprise Trip Around the World?
00:31
Hungry FAM
Рет қаралды 22 МЛН