• Stars
    star
    327
  • Rank 128,393 (Top 3 %)
  • Language
    JavaScript
  • License
    Creative Commons ...
  • Created over 4 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Watched Box

Declarative container queries FTW!

A custom element as a ResizeObserver root. The easiest way to get working with ResizeObserver and creating container queries with JavaScript. Unlike many implementations, you can use any CSS length units, and mix them together.

Get started

Just import the watched-box.min.js module, like so:

import WatchedBox from './path/to/watched-box.min.js';

Then you can start wrapping your content in <watched-box> Custom Elements and defining width and height breakpoints:

<watched-box widthBreaks="70ch, 900px" heightBreaks="50vh, 60em">
  <!-- HTML and text stuff here -->
</watched-box>

This will generate your classes, which take the following format (where a prefix is not supplied; see Props):

  • Less than or equal to the supplied width: w-lte-[the width]
  • Greater than the supplied width: w-gt-[the width]
  • Less than or equal to the supplied height: h-lte-[the height]
  • Greater than the supplied height: h-gt-[the height]

Depending on the aspect ratio, one of the following classes will also be appended (where a prefix is not supplied; see Props):

  • portrait
  • landscape
  • square

Finally, it is recommended you set watched-box to display: block by default in your CSS. You'll rarely be working with inline <watched-box> elements.

watched-box {
  display: block;
}

Examples

Single breaks for each dimension

A box with a width of 405px and a height of 9.5em.

A box that is 405px wide and 9.5em high

<watched-box widthbreaks="405px" heightbreaks="9em" class="w-lte-405px h-gt-9em landscape"></watched-box>

Multiple breaks for each dimension, using different units

A box with a width of 24ch and a height of 120px.

A box that is 24ch wide and 120px high

<watched-box widthbreaks="9em, 800px" heightbreaks="405px, 10vh" class="w-gt-9em w-lte-800px h-lte-405px h-gt-10vh landscape"></watched-box>

Note that units are dynamically converted for comparison, meaning you can mix them. Where the context's value for 1em is 21px, <watched-box> is aware that 3.1em is longer than 63px (21px * 3, or 3em).

Exploring relativity

A box that is 200px squared

This one is more complex, and shows the power of <watched-box>. The element has the following CSS:

watched-box {
  font-size: 20em;
  width: 200px;
  height: 200px;
}

Note the large 20em font-size. This is likely (depending on the context) to make 1em greater than 201px, eliciting the w-lte-1em class as below.

<watched-box widthbreaks="201px, 1em" heightbreaks="199px" class="w-lte-201px w-lte-1em h-gt-199px square"></watched-box>

Here's the same result, but with the prefix="wbx" prop/value supplied:

<watched-box widthbreaks="201px, 1em" heightbreaks="199px" prefix="wbx" class="wbx-w-lte-201px wbx-w-lte-1em wbx-h-gt-199px wbx-square"></watched-box>

Props

Custom elements like <watched-box> take props as attributes. When the values change, <watched-box> reinitializes ResizeObserver observation.

Name Default Description
widthBreaks '1024px' {string} A comma-separated list of CSS length values (e.g. widthBreaks="20vw, 65ch, 300px, 900px")
heightBreaks '768px' {string} A comma-separated list of CSS length values (e.g. heightBreaks="50vh, 800px, 9.25em, 200em")
prefix null {string} A prefix for each generated class, for namespacing purposes (e.g. prefix="wbox")

Workflow

Obviously, <watched-box> doesn't write CSS for you (sorry to disappoint!). However, <watched-box> is just an element, and can be attributed and targeted with CSS however you like.

In the following example, I have created a specific <watched-box> and identified it with the class .grid. Imagining it has a 30ch width breakpoint, I can affect a .cell child element like so:

.grid.w-lte-30ch .cell {
  /* styles for less-than-or-equal-to 30ch width */
}

.grid.w-gt-30ch .cell {
  /* styles for over 30ch width */
}

Naturally, I can use negation too, if I wish:

.grid:not(.w-lte-30ch) .cell {
  /* styles for not less-than-or-equal-to 30ch width */
}

More Repositories

1

inclusive-design-checklist

Aims to be the biggest checklist of inclusive design considerations ever
JavaScript
2,713
star
2

fukol-grids

CSS
1,468
star
3

REVENGE.CSS

A CSS bookmarklet that puts pink error boxes (with messages in comic sans) everywhere you write bad HTML.
CSS
828
star
4

forceFeed

JavaScript
518
star
5

bruck

A prototyping system built with web components and the Houdini Paint API
JavaScript
511
star
6

Community-Icon-Font

To create a collaborative font using SVG
196
star
7

inclusive-menu-button

A keyboard and screen reader accessible ARIA menu button implementation.
JavaScript
191
star
8

design-tokens-cli

Set of design tokens and a custom properties parser for prototyping
JavaScript
173
star
9

cel-animation

A Sass @mixin for creating traditional frame-based animations, especially with SVG
CSS
155
star
10

beadz-drum-machine

A quantum, polymetric drum machine.
Vue
141
star
11

on-demand-live-region

JavaScript
128
star
12

ga11ery

Eleventy theme for showcasing photography and web comics
HTML
97
star
13

cress

JavaScript
83
star
14

readabilityCheckerCLI

A node CLI for checking how readable content is. Accepts a URL or HTML file.
JavaScript
70
star
15

xiao

A tiny, accessible, browser-driven single-page routing system.
JavaScript
56
star
16

react-sortable-table-demo

See the article here: https://inclusive-components.design/data-tables
JavaScript
40
star
17

css-scopulation-enterprise-max-ai

JavaScript
29
star
18

react-theme-switch

JavaScript
27
star
19

Inclusive-Components

Resources for the inclusive components blog (inclusive-components.design)
26
star
20

css-error-property-style

18
star
21

wai-aria-web-component

A "disclosure" (show/hide) web component using ARIA and built with Polymer
CSS
13
star
22

inclusive-inline-editable

JavaScript
9
star
23

buttonlike

A jQuery plugin for making clickable elements more accessible
JavaScript
8
star
24

typical-theme-wordpress

A responsive, resolution independent Wordpress theme with microdata support
CSS
8
star
25

AMA

Ask me anything
6
star
26

Squib-Font

A free font licensed under the Open Font License
6
star
27

heydonworks

HeydonWorks Wordpress theme
CSS
3
star
28

Accessible-Designer-Examples

Examples of creative, accessible design concepts.
HTML
3
star
29

ga11ery-michael

HTML
2
star
30

ga11ery-example

HTML
2
star
31

niace-stories-app

The alpha application for NIACE.
JavaScript
1
star
32

Redfoir

Freedom of Information Timeline Project
JavaScript
1
star