Git Product home page Git Product logo

qwik-breakpoints's Introduction

Qwik Breakpoints

This package provides utilities to build responsive UIs that react to screen-size changes.

useBreakpointSignal

A layout breakpoint is a viewport size threshold at which a layout shift can occur. The viewport size ranges between breakpoints corresponde to different standard screen sizes.

useBreakpointSignal lets you evaluate media queries to determine the current screen size and react to changes when the viewport size crosses a breakpoint.

import { component$, useComputed$ } from '@builder.io/qwik';
import { Breakpoints, useBreakpointSignal } from 'qwik-breakpoints';

export default component$(() => {
  const bpSignal = useBreakpointSignal([
    Breakpoints.XSmall,
    Breakpoints.Small,
    Breakpoints.Medium,
    Breakpoints.Large,
    Breakpoints.XLarge,
  ]);

  const currentScreenSize = useComputed$(() => {
    const displayNameMap = new Map([
      [Breakpoints.XSmall, 'XSmall'],
      [Breakpoints.Small, 'Small'],
      [Breakpoints.Medium, 'Medium'],
      [Breakpoints.Large, 'Large'],
      [Breakpoints.XLarge, 'XLarge'],
    ]);
    for (const query of Object.keys(bpSignal.value.breakpoints)) {
      if (bpSignal.value.breakpoints[query]) {
        return displayNameMap.get(query) ?? 'Unknown';
      }
    }
  });

  return (
    <>
      <p>Resize your browser window to see the current screen size change.</p>
      <p>
        The current screen size is <strong>{currentScreenSize.value}</strong>
      </p>
    </>
  );
});

React to changes to the viewport

const layoutChanges = useBreakpointSignal([
  '(orientation: portrait)',
  '(orientation: landscape)',
]);

useVisibleTask$(({ track }) => {
  track(() => layoutChanges.value);
  updateMyLayoutForOrientationChange();
}

Predefined breakpoints

The built-in Breakpoints constant offers the following predefinded breakpoints for convenience, originally drawn from the Material Design specification.

Breakpoint name Media query
XSmall (max-width: 599.98px)
Small (min-width: 600px) and (max-width: 959.98px)
Medium (min-width: 960px) and (max-width: 1279.98px)
Large (min-width: 1280px) and (max-width: 1919.98px)
XLarge (min-width: 1920px)
Handset (max-width: 599.98px) and (orientation: portrait), (max-width: 959.98px) and (orientation: landscape)
Tablet (min-width: 600px) and (max-width: 839.98px) and (orientation: portrait), (min-width: 960px) and (max-width: 1279.98px) and (orientation: landscape)
Web (min-width: 840px) and (orientation: portrait), (min-width: 1280px) and (orientation: landscape)
HandsetPortrait (max-width: 599.98px) and (orientation: portrait)
TabletPortrait (min-width: 600px) and (max-width: 839.98px) and (orientation: portrait)
WebPortrait (min-width: 840px) and (orientation: portrait)
HandsetLandscape (max-width: 959.98px) and (orientation: landscape)
TabletLandscape (min-width: 960px) and (max-width: 1279.98px) and (orientation: landscape)
WebLandscape (min-width: 1280px) and (orientation: landscape)

You can use these predefined breakpoints with useBreakpointSignal.

const result = useBreakpointSignal([
  Breakpoints.HandsetLandscape,
  Breakpoints.HandsetPortrait
]);

return {
  result.value.matches
  ? <div>Handset</div>
  : <div>Not Handset</div>
}

qwik-breakpoints's People

Contributors

github-actions[bot] avatar ulic75 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

jreilly-lukava

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.