
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
breakpoint-changes-rx
Advanced tools
[![NPM version][npm-image]][npm-url] [![Downloads][npm-downloads-image]][npm-url] [![star this repo][gh-stars-image]][gh-url] [![fork this repo][gh-forks-image]][gh-url] [![Build Status][gh-status-image]][gh-url] [![Coverage Status][coveralls-image]][cove
Detect and handle breakpoint changes using RxJS Observables
This package helps to detect current breakpoints and breakpoint changes. It initializes with a breakpoint configuration and provides Observables and useful functions. Multiple breakpoints at the same time are supported.
⚠️️️️⚠️⚠️ WARNING: Version 3.x drops support for CommonJS and AMD. Target is ES2017 ⚠️⚠️⚠️
Version 3.0.0 only ships as ES module. Also the target version of this package is ES2017. If you need to support older browsers you need to transpile to a prior ES version.
⚠️️️️⚠️⚠️ WARNING: Version 2.x and beyond ⚠️⚠️⚠️
Version 2.0.0 removed the usage of the deprecated MediaQueryList.addListener. Instead it uses MediaQueryList.onchange. See compatibility table before using it. If needed I will continue provide changes to version 1.x which is using the deprecated function.
yarn add breakpoint-changes-rx
breakpoints(breakpointDefinitions)
This function initializes the breakpoint detection and returns an object containing following properties:
ℹ️
window.matchMedia()
and MediaQueryList.onchange are used so only browsers supporting this features can be used.
For details about the breakpointDefinitions see Configuration section
import breakpoints, { parseBreakpoints } from 'breakpoint-changes-rx';
import style from './style.scss';
const breakpointDefinitions = parseBreakpoints(style);
const bps = breakpoints(breakpointDefinitions);
console.log('current breakpoints: %o', bps.getCurrentBreakpoints());
breakpointsChanges$
An observable that emits a value when breakpoints change. The format of these values is
{
curr: ['md'],
prev: ['lg'],
}
Usage example:
bps.breakpointChanges$.subscribe(({ curr, prev }) => {
console.log('previous breakpoint is %o, actual breakpoint is %o', prev, curr);
});
Current and previous breakpoints are stored in arrays because it's possible to have multiple active breakpoints at the same time.
breakpointsChangesBehavior$
This is the same as breakpointsChanges$
except it is a BehaviorSubject
. So when a new Observer subscribes it will immediately emit the current breakpoint data.
getCurrentBreakpoints()
Returns the current active breakpoints as an array.
const current = bp.getCurrentBreakpoints();
console.log('current breakpoints are %o', current); // e.g. ['lg']
breakpointsChange(bp)
Returns an observable that emits a boolean
value when the given breakpoint gets entered or left.
bp.breakpointsChange('md').subscribe((active) => {
console.log('breakpoint md %s', active ? 'entered' : 'left');
});
breakpointsInRange(range)
Returns an observable that emits a boolean
value when a range of breakpoints gets entered or left. If a change appears between the given range no value gets emitted.
bp.breakpointsInRange(['sm', 'md']).subscribe((active) => {
console.log('range gets %s', active ? 'entered' : 'left');
});
includesBreakpoints(bps)
Returns true
if the current breakpoints contain breakpoints which are part of the given array
.
console.log('this might be a mobile device %o', bp.includesBreakpoints(['sm', 'md']));
includesBreakpoint(bp)
Returns true
if the given breakpoint is part of the current active breakpoints.
console.log('breakpoint "md" is active %o', bp.includesBreakpoint('md'));
This section describes the configuration used by the breakpoints(breakpointDefinitions) function using this example
Breakpoint name | min | max |
---|---|---|
sm | 767px | |
md | 768px | 991px |
lg | 991px | 1199px |
xl | 1200px |
The used format for this example is shown here:
{
"sm": { "max": "767px", },
"md": { "min": "768px", "max": "991px" },
"lg": { "min": "992px", "max": "1199px" },
"xl": { "min": "1200px" }
}
The values for the min
and max
properties can also be of type number
. In this case pixels as unit will be omitted.
parseBreakpoints(object, config)
This function was created to parse breakpoint data from exported variables of a CSS Module.
:export {
breakpoint-sm-max: $bp-small-max;
breakpoint-md-min: $bp-medium;
breakpoint-md-max: $bp-medium-max;
breakpoint-lg-min: $bp-large;
breakpoint-lg-max: $bp-large-max;
breakpoint-xl-min: $bp-xlarge;
}
import { parseBreakpoints } from 'breakpoint-changes-rx';
import style from './style.scss';
const breakpointDefinitions = parseBreakpoints(style);
It filters all properties of the passed object that matches the breakpoint pattern.
This function can use an optional configuration.
Name | Description | Default |
---|---|---|
regex | A regular expression describing the breakpoint naming to parse | /^breakpoint-(\w*)-((max)|(min))$/ |
groupName | The index of the capture group that contains the name of the breakpoint | 1 |
groupMinMax | The capture group index that contains the identifier for min or max of the breakpoint | 2 |
isMin | A function that returns true if the given min/max value represents min | (val) => val === nameMin |
MIT © 2022 Jens Simon
FAQs
[![NPM version][npm-image]][npm-url] [![Downloads][npm-downloads-image]][npm-url] [![star this repo][gh-stars-image]][gh-url] [![fork this repo][gh-forks-image]][gh-url] [![CI][gh-status-image]][gh-status-url] [![Coverage Status][coveralls-image]][coveral
The npm package breakpoint-changes-rx receives a total of 167 weekly downloads. As such, breakpoint-changes-rx popularity was classified as not popular.
We found that breakpoint-changes-rx demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.