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

docs: Adding Example for useOnWindow #4878

Merged
merged 4 commits into from
Aug 27, 2023

Conversation

josemartinez111
Copy link
Contributor

Overview

This PR provides a practical example for the useOnWindow hook and useSignal for managing component state in the Qwik framework.

The code introduces a custom hook called useCloseDropdown, which manages the state of a dropdown component. The dropdown can be opened by clicking a button and is automatically closed when a click event happens outside of the dropdown button.

Here's how it works:

  • Two signals, isOpen and dropdownToggleBtn, are created. isOpen is a boolean signal for managing the open/close state of the dropdown. dropdownToggleBtn is used to hold a reference to the dropdown button element.

  • An event listener closeDropdown is defined, which is invoked whenever a click event happens on the window. If the clicked element is not the dropdown button or its descendants, we set isOpen.value to false, effectively closing the dropdown.

  • The useOnWindow hook is used to attach the closeDropdown event listener to the window.

In our component, we use the setDropdownToggleBtnRef function to assign the button DOM element to the dropdownToggleBtn signal.

The utility of this example is to demonstrate how one can use the useOnWindow hook and signals to interact with the DOM directly and manage component state based on user interaction and events outside the component itself.

… example for the docs

This PR provides a practical example for the useOnWindow hook and useSignal for managing component
state in the Qwik framework.
@stackblitz
Copy link

stackblitz bot commented Jul 27, 2023

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@netlify
Copy link

netlify bot commented Jul 27, 2023

👷 Deploy request for qwik-insights pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 05ffa06

@zanettin zanettin changed the title Adding Example for useOnWindow docs: Adding Example for useOnWindow Jul 28, 2023
@zanettin zanettin added the COMP: docs Improvements or additions to documentation label Jul 28, 2023
@zanettin zanettin added the STATUS-2: PR waiting for review This PR is waiting for review and approval before merge label Aug 18, 2023
Copy link
Contributor

@zanettin zanettin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx @josemartinez111 for creating this 🙏 just adjusted the path so it's resolvable (qwik works with directories and an index file in it instead of file namings).

@zanettin zanettin enabled auto-merge (squash) August 27, 2023 20:43
@zanettin zanettin merged commit ac1e4af into QwikDev:main Aug 27, 2023
18 checks passed
@josemartinez111
Copy link
Contributor Author

josemartinez111 commented Sep 13, 2023

thx @josemartinez111 for creating this 🙏 just adjusted the path so it's resolvable (qwik works with directories and an index file in it instead of file namings).

Thank you @zanettin for approving my very first contribution 🙌🏾

@josemartinez111 josemartinez111 deleted the dev/use-on-window branch September 13, 2023 18:48
@zanettin
Copy link
Contributor

thank YOU @josemartinez111 for your time and contribution 🙏

kodiakhq bot referenced this pull request in ascorbic/unpic-img Sep 17, 2023
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [@builder.io/qwik](https://qwik.builder.io/) ([source](https://togithub.com/BuilderIO/qwik)) | [`1.2.10` -> `1.2.11`](https://renovatebot.com/diffs/npm/@builder.io%2fqwik/1.2.10/1.2.11) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@builder.io%2fqwik/1.2.11?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@builder.io%2fqwik/1.2.11?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@builder.io%2fqwik/1.2.10/1.2.11?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@builder.io%2fqwik/1.2.10/1.2.11?slim=true)](https://docs.renovatebot.com/merge-confidence/) |

---

### Release Notes

<details>
<summary>BuilderIO/qwik (@&#8203;builder.io/qwik)</summary>

### [`v1.2.11`](https://togithub.com/BuilderIO/qwik/releases/tag/v1.2.11)

[Compare Source](https://togithub.com/BuilderIO/qwik/compare/v1.2.10...v1.2.11)

##### What's Changed

-   Add useId() docs and demo component by [@&#8203;n8sabes](https://togithub.com/n8sabes) in [https://github.com/BuilderIO/qwik/pull/5037](https://togithub.com/BuilderIO/qwik/pull/5037)
-   add n8sabes to contributors by [@&#8203;gioboa](https://togithub.com/gioboa) in [https://github.com/BuilderIO/qwik/pull/5040](https://togithub.com/BuilderIO/qwik/pull/5040)
-   chore: array index can be typed as number by [@&#8203;wtlin1228](https://togithub.com/wtlin1228) in [https://github.com/BuilderIO/qwik/pull/4748](https://togithub.com/BuilderIO/qwik/pull/4748)
-   Modular Forms: Fix incorrect type in example by [@&#8203;RaeesBhatti](https://togithub.com/RaeesBhatti) in [https://github.com/BuilderIO/qwik/pull/5036](https://togithub.com/BuilderIO/qwik/pull/5036)
-   docs: add docs about server$ middleware by [@&#8203;RaeesBhatti](https://togithub.com/RaeesBhatti) in [https://github.com/BuilderIO/qwik/pull/5033](https://togithub.com/BuilderIO/qwik/pull/5033)
-   feat: add jsx-a eslint rule by [@&#8203;kerbelp](https://togithub.com/kerbelp) in [https://github.com/BuilderIO/qwik/pull/4256](https://togithub.com/BuilderIO/qwik/pull/4256)
-   docs: Adding Example for useOnWindow  by [@&#8203;josemartinez111](https://togithub.com/josemartinez111) in [https://github.com/BuilderIO/qwik/pull/4878](https://togithub.com/BuilderIO/qwik/pull/4878)
-   chore: supabase-auth-helpers-qwik: update devDependencies by [@&#8203;gioboa](https://togithub.com/gioboa) in [https://github.com/BuilderIO/qwik/pull/5041](https://togithub.com/BuilderIO/qwik/pull/5041)
-   fix: action type by [@&#8203;wtlin1228](https://togithub.com/wtlin1228) in [https://github.com/BuilderIO/qwik/pull/5034](https://togithub.com/BuilderIO/qwik/pull/5034)
-   chore: add more debug info to build by [@&#8203;mhevery](https://togithub.com/mhevery) in [https://github.com/BuilderIO/qwik/pull/5047](https://togithub.com/BuilderIO/qwik/pull/5047)
-   fix: align `function` and `()=>` behavior for JSX transpilation by [@&#8203;mhevery](https://togithub.com/mhevery) in [https://github.com/BuilderIO/qwik/pull/5012](https://togithub.com/BuilderIO/qwik/pull/5012)
-   fix(core): correctly handle serialization of Signal of X and naked X by [@&#8203;mhevery](https://togithub.com/mhevery) in [https://github.com/BuilderIO/qwik/pull/5013](https://togithub.com/BuilderIO/qwik/pull/5013)
-   fix(core): pass through decorators by [@&#8203;mhevery](https://togithub.com/mhevery) in [https://github.com/BuilderIO/qwik/pull/5052](https://togithub.com/BuilderIO/qwik/pull/5052)
-   fix(qwik-city): correct rest route regression by [@&#8203;mhevery](https://togithub.com/mhevery) in [https://github.com/BuilderIO/qwik/pull/5054](https://togithub.com/BuilderIO/qwik/pull/5054)
-   fix: QwikInsights path resolution by [@&#8203;gioboa](https://togithub.com/gioboa) in [https://github.com/BuilderIO/qwik/pull/5051](https://togithub.com/BuilderIO/qwik/pull/5051)
-   docs: add qwikInsights vite plugin by [@&#8203;gioboa](https://togithub.com/gioboa) in [https://github.com/BuilderIO/qwik/pull/5055](https://togithub.com/BuilderIO/qwik/pull/5055)
-   docs: change [@&#8203;builder](https://togithub.com/builder).io/qwik-labs version by [@&#8203;gioboa](https://togithub.com/gioboa) in [https://github.com/BuilderIO/qwik/pull/5057](https://togithub.com/BuilderIO/qwik/pull/5057)
-   docs: add prettier as devDependencies ([#&#8203;5059](https://togithub.com/BuilderIO/qwik/issues/5059)) by [@&#8203;gioboa](https://togithub.com/gioboa) in [https://github.com/BuilderIO/qwik/pull/5061](https://togithub.com/BuilderIO/qwik/pull/5061)
-   chore: fix .nvmrc with the right version by [@&#8203;gioboa](https://togithub.com/gioboa) in [https://github.com/BuilderIO/qwik/pull/5060](https://togithub.com/BuilderIO/qwik/pull/5060)
-   docs: Document Relative URLs in Loaders by [@&#8203;n8sabes](https://togithub.com/n8sabes) in [https://github.com/BuilderIO/qwik/pull/5063](https://togithub.com/BuilderIO/qwik/pull/5063)
-   docs: Cookbook recipe for playing audio and video media on iOS devices by [@&#8203;n8sabes](https://togithub.com/n8sabes) in [https://github.com/BuilderIO/qwik/pull/5066](https://togithub.com/BuilderIO/qwik/pull/5066)
-   Revert "docs: Cookbook recipe for playing audio and video media on iOS devices" by [@&#8203;mhevery](https://togithub.com/mhevery) in [https://github.com/BuilderIO/qwik/pull/5073](https://togithub.com/BuilderIO/qwik/pull/5073)
-   fix(qwik-city): do not write into a closed stream by [@&#8203;mhevery](https://togithub.com/mhevery) in [https://github.com/BuilderIO/qwik/pull/5074](https://togithub.com/BuilderIO/qwik/pull/5074)
-   fix: loader type by [@&#8203;wtlin1228](https://togithub.com/wtlin1228) in [https://github.com/BuilderIO/qwik/pull/5062](https://togithub.com/BuilderIO/qwik/pull/5062)
-   Add media controller to cookbook by [@&#8203;n8sabes](https://togithub.com/n8sabes) in [https://github.com/BuilderIO/qwik/pull/5078](https://togithub.com/BuilderIO/qwik/pull/5078)
-   Fix the loader document by [@&#8203;n8sabes](https://togithub.com/n8sabes) in [https://github.com/BuilderIO/qwik/pull/5077](https://togithub.com/BuilderIO/qwik/pull/5077)
-   feat: i18n $localize starter by [@&#8203;gioboa](https://togithub.com/gioboa) in [https://github.com/BuilderIO/qwik/pull/5070](https://togithub.com/BuilderIO/qwik/pull/5070)
-   Loader doc consistency by [@&#8203;n8sabes](https://togithub.com/n8sabes) in [https://github.com/BuilderIO/qwik/pull/5082](https://togithub.com/BuilderIO/qwik/pull/5082)
-   Enhance Media Controller Cookbook Recipe and fix fluid layout for smaller screens by [@&#8203;n8sabes](https://togithub.com/n8sabes) in [https://github.com/BuilderIO/qwik/pull/5081](https://togithub.com/BuilderIO/qwik/pull/5081)
-   Remove overlooked console.log() from cookbook recipe by [@&#8203;n8sabes](https://togithub.com/n8sabes) in [https://github.com/BuilderIO/qwik/pull/5083](https://togithub.com/BuilderIO/qwik/pull/5083)
-   Update cookbook to stream 1080p video content by [@&#8203;n8sabes](https://togithub.com/n8sabes) in [https://github.com/BuilderIO/qwik/pull/5084](https://togithub.com/BuilderIO/qwik/pull/5084)
-   Update pages.json by [@&#8203;blove](https://togithub.com/blove) in [https://github.com/BuilderIO/qwik/pull/5085](https://togithub.com/BuilderIO/qwik/pull/5085)
-   fix: library starter don't bundle dependencies by [@&#8203;wmertens](https://togithub.com/wmertens) in [https://github.com/BuilderIO/qwik/pull/2008](https://togithub.com/BuilderIO/qwik/pull/2008)
-   Fixed typo in Docs > Integrations > Prisma by [@&#8203;adcar](https://togithub.com/adcar) in [https://github.com/BuilderIO/qwik/pull/5090](https://togithub.com/BuilderIO/qwik/pull/5090)
-   starter: Update deps and code of supabase auth helpers qwik pkg by [@&#8203;diecodev](https://togithub.com/diecodev) in [https://github.com/BuilderIO/qwik/pull/4745](https://togithub.com/BuilderIO/qwik/pull/4745)
-   docs: Image updates - Qwik City  by [@&#8203;hamatoyogi](https://togithub.com/hamatoyogi) in [https://github.com/BuilderIO/qwik/pull/5098](https://togithub.com/BuilderIO/qwik/pull/5098)
-   docs: update the basic projection tutorial by [@&#8203;wtlin1228](https://togithub.com/wtlin1228) in [https://github.com/BuilderIO/qwik/pull/5093](https://togithub.com/BuilderIO/qwik/pull/5093)
-   docs: update the implicit reactivity tutorial by [@&#8203;wtlin1228](https://togithub.com/wtlin1228) in [https://github.com/BuilderIO/qwik/pull/5092](https://togithub.com/BuilderIO/qwik/pull/5092)
-   docs: remove outdated props store tutorial by [@&#8203;wtlin1228](https://togithub.com/wtlin1228) in [https://github.com/BuilderIO/qwik/pull/5095](https://togithub.com/BuilderIO/qwik/pull/5095)
-   fix: remove missing tutorial ([#&#8203;5105](https://togithub.com/BuilderIO/qwik/issues/5105)) by [@&#8203;gioboa](https://togithub.com/gioboa) in [https://github.com/BuilderIO/qwik/pull/5106](https://togithub.com/BuilderIO/qwik/pull/5106)
-   docs: fix "Endpoints" link on Qwik City -> Routing page by [@&#8203;jakovljevic-mladen](https://togithub.com/jakovljevic-mladen) in [https://github.com/BuilderIO/qwik/pull/5107](https://togithub.com/BuilderIO/qwik/pull/5107)
-   chore: release qwik-auth 0.1.1 by [@&#8203;ulic75](https://togithub.com/ulic75) in [https://github.com/BuilderIO/qwik/pull/5103](https://togithub.com/BuilderIO/qwik/pull/5103)
-   docs: update reactivity's counter example by [@&#8203;wtlin1228](https://togithub.com/wtlin1228) in [https://github.com/BuilderIO/qwik/pull/5091](https://togithub.com/BuilderIO/qwik/pull/5091)
-   chore(eslint-plugin-qwik): change eslint to ^8.45 by [@&#8203;adaliszk](https://togithub.com/adaliszk) in [https://github.com/BuilderIO/qwik/pull/5110](https://togithub.com/BuilderIO/qwik/pull/5110)
-   docs: fix mismatch title for implicit reactivity tutorial by [@&#8203;wtlin1228](https://togithub.com/wtlin1228) in [https://github.com/BuilderIO/qwik/pull/5111](https://togithub.com/BuilderIO/qwik/pull/5111)
-   feat(optimizer): add qwikVite configuration option optimizerOptions.inlineStylesUpToBytes by [@&#8203;jaroel](https://togithub.com/jaroel) in [https://github.com/BuilderIO/qwik/pull/5089](https://togithub.com/BuilderIO/qwik/pull/5089)
-   fix(qwikcity): Fix route-matcher regression [#&#8203;5080](https://togithub.com/BuilderIO/qwik/issues/5080) by [@&#8203;mhevery](https://togithub.com/mhevery) in [https://github.com/BuilderIO/qwik/pull/5112](https://togithub.com/BuilderIO/qwik/pull/5112)
-   docs(authjs): use pathname in protected route example by [@&#8203;ulic75](https://togithub.com/ulic75) in [https://github.com/BuilderIO/qwik/pull/5115](https://togithub.com/BuilderIO/qwik/pull/5115)
-   starters: Ensure that VSCode emmet uses JSX syntax for self-closing tags by [@&#8203;colelawrence](https://togithub.com/colelawrence) in [https://github.com/BuilderIO/qwik/pull/5116](https://togithub.com/BuilderIO/qwik/pull/5116)
-   fix(eslint): Support explicit export with loader-location by [@&#8203;mhevery](https://togithub.com/mhevery) in [https://github.com/BuilderIO/qwik/pull/5121](https://togithub.com/BuilderIO/qwik/pull/5121)
-   fix v1.2 playground by [@&#8203;wmertens](https://togithub.com/wmertens) in [https://github.com/BuilderIO/qwik/pull/5124](https://togithub.com/BuilderIO/qwik/pull/5124)
-   feat: Add route rewriting by [@&#8203;claudioshiver](https://togithub.com/claudioshiver) in [https://github.com/BuilderIO/qwik/pull/5122](https://togithub.com/BuilderIO/qwik/pull/5122)
-   docs(qwik-city): Add docs for routes rewriting by [@&#8203;claudioshiver](https://togithub.com/claudioshiver) in [https://github.com/BuilderIO/qwik/pull/5127](https://togithub.com/BuilderIO/qwik/pull/5127)
-   bug(vite): populate rollup cache after transform by [@&#8203;wmertens](https://togithub.com/wmertens) in [https://github.com/BuilderIO/qwik/pull/5130](https://togithub.com/BuilderIO/qwik/pull/5130)
-   feat(qrl): add resolved by [@&#8203;wmertens](https://togithub.com/wmertens) in [https://github.com/BuilderIO/qwik/pull/5118](https://togithub.com/BuilderIO/qwik/pull/5118)
-   test(qwik-city): add route-matcher case with two rest params by [@&#8203;mhevery](https://togithub.com/mhevery) in [https://github.com/BuilderIO/qwik/pull/5133](https://togithub.com/BuilderIO/qwik/pull/5133)
-   Issue 5065 by [@&#8203;mhevery](https://togithub.com/mhevery) in [https://github.com/BuilderIO/qwik/pull/5131](https://togithub.com/BuilderIO/qwik/pull/5131)
-   fix(qwik-city): Correctly SSG routes even with `trailingSlash: false` by [@&#8203;mhevery](https://togithub.com/mhevery) in [https://github.com/BuilderIO/qwik/pull/5132](https://togithub.com/BuilderIO/qwik/pull/5132)
-   Issue 5126 by [@&#8203;mhevery](https://togithub.com/mhevery) in [https://github.com/BuilderIO/qwik/pull/5135](https://togithub.com/BuilderIO/qwik/pull/5135)
-   chore(insights): upgrade drizzle by [@&#8203;mhevery](https://togithub.com/mhevery) in [https://github.com/BuilderIO/qwik/pull/5134](https://togithub.com/BuilderIO/qwik/pull/5134)
-   fix(qwik-city): correct validator failed type by [@&#8203;wtlin1228](https://togithub.com/wtlin1228) in [https://github.com/BuilderIO/qwik/pull/5137](https://togithub.com/BuilderIO/qwik/pull/5137)
-   fix(insight): Correct DB returning too much data error by [@&#8203;mhevery](https://togithub.com/mhevery) in [https://github.com/BuilderIO/qwik/pull/5139](https://togithub.com/BuilderIO/qwik/pull/5139)
-   Revert "chore(insights): upgrade drizzle" by [@&#8203;mhevery](https://togithub.com/mhevery) in [https://github.com/BuilderIO/qwik/pull/5138](https://togithub.com/BuilderIO/qwik/pull/5138)
-   docs: improve descriptions for aws, azure and gcp integrations by [@&#8203;WilliamEspegren](https://togithub.com/WilliamEspegren) in [https://github.com/BuilderIO/qwik/pull/5144](https://togithub.com/BuilderIO/qwik/pull/5144)
-   feat: provide compatibility with angular by [@&#8203;dmitry-stepanenko](https://togithub.com/dmitry-stepanenko) in [https://github.com/BuilderIO/qwik/pull/5119](https://togithub.com/BuilderIO/qwik/pull/5119)
-   1.2.11 by [@&#8203;mhevery](https://togithub.com/mhevery) in [https://github.com/BuilderIO/qwik/pull/5145](https://togithub.com/BuilderIO/qwik/pull/5145)

##### New Contributors

-   [@&#8203;josemartinez111](https://togithub.com/josemartinez111) made their first contribution in [https://github.com/BuilderIO/qwik/pull/4878](https://togithub.com/BuilderIO/qwik/pull/4878)
-   [@&#8203;blove](https://togithub.com/blove) made their first contribution in [https://github.com/BuilderIO/qwik/pull/5085](https://togithub.com/BuilderIO/qwik/pull/5085)
-   [@&#8203;adcar](https://togithub.com/adcar) made their first contribution in [https://github.com/BuilderIO/qwik/pull/5090](https://togithub.com/BuilderIO/qwik/pull/5090)
-   [@&#8203;diecodev](https://togithub.com/diecodev) made their first contribution in [https://github.com/BuilderIO/qwik/pull/4745](https://togithub.com/BuilderIO/qwik/pull/4745)
-   [@&#8203;adaliszk](https://togithub.com/adaliszk) made their first contribution in [https://github.com/BuilderIO/qwik/pull/5110](https://togithub.com/BuilderIO/qwik/pull/5110)
-   [@&#8203;colelawrence](https://togithub.com/colelawrence) made their first contribution in [https://github.com/BuilderIO/qwik/pull/5116](https://togithub.com/BuilderIO/qwik/pull/5116)
-   [@&#8203;claudioshiver](https://togithub.com/claudioshiver) made their first contribution in [https://github.com/BuilderIO/qwik/pull/5122](https://togithub.com/BuilderIO/qwik/pull/5122)
-   [@&#8203;WilliamEspegren](https://togithub.com/WilliamEspegren) made their first contribution in [https://github.com/BuilderIO/qwik/pull/5144](https://togithub.com/BuilderIO/qwik/pull/5144)

**Full Changelog**: QwikDev/qwik@v1.2.10...v1.2.11

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "after 9pm on sunday" (UTC), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/ascorbic/unpic-img).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNi44My4wIiwidXBkYXRlZEluVmVyIjoiMzYuODMuMCIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==-->
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-2: PR waiting for review This PR is waiting for review and approval before merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants