Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ericrovell/svelte-media-observer

SvelteJS media queries observer.
https://github.com/ericrovell/svelte-media-observer

media-queries observable observer svelte sveltejs sveltekit

Last synced: about 21 hours ago
JSON representation

SvelteJS media queries observer.

Awesome Lists containing this project

README

        


Alien with one eye depicting observer



build action status



npm package version


types included


downloads count


node version


licence



minified size


minzipped size


dependency count


tree-shaking

# Svelte Media Observer

Svelte Media observer is small utility media queries observer built for [Svelte](https://svelte.dev/) framework.

Try it in the [Svelte REPL](https://svelte.dev/repl/e9f6a16dfd9a4013b1a0a76347b206eb?version=3).

## Getting started

The package is available via [npm](https://www.npmjs.com/package/svelte-media-observer):

```
npm i svelte-media-observer
```

## Usage

```ts
import { mediaObserver } from "svelte-media-observer";
import type { MediaQueries } from "svelte-media-observer";

const mediaQueries: MediaQueries<"small" | "medium" | "dark"> = [
[ "small", "(max-width: 640px)" ],
[ "medium", "(max-width: 935px)" ],
[ "dark", "(prefers-color-scheme: dark)" ]
];

export const media = mediaObserver(mediaQueries);
```

observer contains all named media queries as `Record`. It is build on [svelte/stores](https://svelte.dev/docs#svelte_store), so the usage is the same:

```html

import { media } from "your-observer-path";

{#if $media.small}

{:else}

{/if}
```

## Note

It is a nice idea to create several observers depending on your need and the rate the media queries will change.

## Issues

While using Typescript and ESLint for Svelte, you might meet some [limitations](https://github.com/sveltejs/eslint-plugin-svelte3#installation-with-typescript) to type-aware rules. [Link](https://github.com/sveltejs/eslint-plugin-svelte3/issues/89) to the issue.

```html

import { media } from "your-observer-path";

// incorrect no-unsafe-member-access error
{#if $media.small}

{:else}

{/if}
```

## Attribution

Observer alien was created by [FreakAddL](https://t.me/portfolio_e_g).