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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[馃摉] Hooks Cookbook #6136

Open
dejurin opened this issue Apr 19, 2024 · 2 comments
Open

[馃摉] Hooks Cookbook #6136

dejurin opened this issue Apr 19, 2024 · 2 comments
Labels
COMP: docs Improvements or additions to documentation STATUS-1: needs triage New issue which needs to be triaged

Comments

@dejurin
Copy link
Contributor

dejurin commented Apr 19, 2024

Suggestion

Hello, thank you for taking the time to read my proposal.

I found that there are no ready-made hooks for everyday life in the documentation. I propose to create, something like, a custom hooks cookbook. And add them from time to time.

Hook for example:

import type { QRL, Signal } from "@builder.io/qwik";
import { $, useOnDocument } from "@builder.io/qwik";

/**
 * This code defines a function useClickOutside that listens for a click event on the document. 
 * It checks if the clicked element is not within the specified ref element and then calls the function specified by onClickOut.
 *
 * @param {Signal<HTMLElement | undefined>} ref - The reference to the HTMLElement or undefined.
 * @param {QRL<() => void>} onClickOut - The QRL of the function to be called when clicked outside.
 */
export const useClickOutside = (
  ref: Signal<HTMLElement | undefined>,
  onClickOut: QRL<() => void>,
) => {
  useOnDocument(
    "click",
    $((event) => {
      if (!ref.value) {
        return;
      }
      const target = event.target as HTMLElement;
      if (!ref.value.contains(target)) {
        onClickOut();
      }
    }),
  );
};
@dejurin dejurin added COMP: docs Improvements or additions to documentation STATUS-1: needs triage New issue which needs to be triaged labels Apr 19, 2024
@lieranderl
Copy link
Contributor

There is a separate lib for qwik hooks:
https://www.qwikhooks.dev/

@ditadi
Copy link

ditadi commented Apr 29, 2024

useClickOutside implemented on qwikhooks, thanks to @dejurin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
COMP: docs Improvements or additions to documentation STATUS-1: needs triage New issue which needs to be triaged
Projects
None yet
Development

No branches or pull requests

3 participants