1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 07:08:01 +00:00

Add Check Media Queries From JavaScript as a JavaScript TIL

This commit is contained in:
jbranchaud
2024-10-12 12:30:07 -05:00
parent aa00c55b06
commit ab9d2b5bf6
2 changed files with 30 additions and 1 deletions

View File

@@ -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). For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
_1462 TILs and counting..._ _1463 TILs and counting..._
--- ---
@@ -449,6 +449,7 @@ _1462 TILs and counting..._
- [Check If A Number Is Positive Or Negative](javascript/check-if-a-number-is-positive-or-negative.md) - [Check If A Number Is Positive Or Negative](javascript/check-if-a-number-is-positive-or-negative.md)
- [Check If File Exists Before Reading It](javascript/check-if-file-exists-before-reading-it.md) - [Check If File Exists Before Reading It](javascript/check-if-file-exists-before-reading-it.md)
- [Check If Something Is An Array](javascript/check-if-something-is-an-array.md) - [Check If Something Is An Array](javascript/check-if-something-is-an-array.md)
- [Check Media Queries From JavaScript](javascript/check-media-queries-from-javascript.md)
- [Check The Password Confirmation With Yup](javascript/check-the-password-confirmation-with-yup.md) - [Check The Password Confirmation With Yup](javascript/check-the-password-confirmation-with-yup.md)
- [Compare The Equality Of Two Date Objects](javascript/compare-the-equality-of-two-date-objects.md) - [Compare The Equality Of Two Date Objects](javascript/compare-the-equality-of-two-date-objects.md)
- [Computed Property Names In ES6](javascript/computed-property-names-in-es6.md) - [Computed Property Names In ES6](javascript/computed-property-names-in-es6.md)

View File

@@ -0,0 +1,28 @@
# Check Media Queries From JavaScript
I'm usually thinking about and [using media
queries](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_media_queries/Using_media_queries)
from a CSS context. I use them to control what styles are displayed for a
variety of scenarios, such as at different screen widths, when a user prefers
reduced motion, or when the user prefers a dark color scheme.
The current value of various media queries can be checked from a JavaScript
context as well.
For instance, if we want to see if the user prefers a _dark_ color schema, we
can look for a _match_ on that media query with
[`matchMedia`](https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia).
```javascript
> window.matchMedia('(prefers-color-scheme: dark)')
MediaQueryList {media: '(prefers-color-scheme: dark)', matches: true, onchange: null}
> window.matchMedia('(prefers-color-scheme: dark)')['matches']
true
```
This queries for the [`prefers-color-scheme` media
feature](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme).
The [Astro.build Blog
Tutorial](https://docs.astro.build/en/tutorial/6-islands/2/#add-client-side-interactivity)
shows an example of using this to wire up a Light/Dark mode toggle.