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

Simplify 'is-mobile.svelte.ts|js' hook #1651

Closed
1 task done
developedbyant opened this issue Jan 26, 2025 · 1 comment
Closed
1 task done

Simplify 'is-mobile.svelte.ts|js' hook #1651

developedbyant opened this issue Jan 26, 2025 · 1 comment
Labels
type: feature Introduction of new functionality to the application

Comments

@developedbyant
Copy link

Prerequisites

  • This feature already exists in shadcn/ui - if not, it won't be considered here so don't continue with your issue.

Describe the feature

Why not make is-mobile.svelte.ts file smaller using MediaQuery from svelte/reactivity.

import { MediaQuery } from "svelte/reactivity";
const MOBILE_BREAKPOINT = 768;

export class IsMobile {
	/** Indicate if we are on mobile screen size (700px) or less */
	current = $derived(new MediaQuery(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`).current);
}

current way

import { untrack } from "svelte";

const MOBILE_BREAKPOINT = 768;

export class IsMobile {
	#current = $state<boolean>(false);

	constructor() {
		$effect(() => {
			return untrack(() => {
				const mql = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`);
				const onChange = () => {
					this.#current = window.innerWidth < MOBILE_BREAKPOINT;
				};
				mql.addEventListener("change", onChange);
				onChange();
				return () => {
					mql.removeEventListener("change", onChange);
				};
			});
		});
	}

	get current() {
		return this.#current;
	}
}
@developedbyant developedbyant added the type: feature Introduction of new functionality to the application label Jan 26, 2025
@shyakadavis
Copy link
Contributor

Hi;

Pretty sure this was already done. See: https://github.com/huntabyte/shadcn-svelte/pull/1616/files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature Introduction of new functionality to the application
Projects
None yet
Development

No branches or pull requests

3 participants