StringResponsive Module
Switch breakpoints declaratively. StringResponsive evaluates viewport width with matchMedia, toggles DOM fragments tagged for each device band, and emits boolean events whenever a range flips on or off.
Activation primer: Register
StringResponsiveglobally viastringTune.use(StringResponsive)—no per-elementstring="responsive"marker is required, andstring-idis not used. The attributes below simply declare which breakpoints should reveal each fragment.
HTML Attributes
| Attribute | Applies to | Default display | Purpose | Practical notes |
|---|---|---|---|---|
string-mobile | Any element block | inline/block | Show the element when the viewport is mobile | Hidden via display: none outside the active range |
string-tablet | Any element block | inline/block | Show while tablet media query matches | You can combine multiple flags to share content across bands |
string-laptop | Any element block | inline/block | Show while laptop media query matches | Useful for medium desktop layouts |
string-desktop | Any element block | inline/block | Show for wide desktops | Pair with string-laptop for progressive enhancements |
The module hides ineligible elements by applying
style.display = 'none'. Inlinedisplaystyles you set will be restored when the range is active.
Module Snapshot
- Activation: register globally via
stringTune.use(StringResponsive)(no per-elementstringattribute) - Default ranges: mobile ≤ 359 px, tablet 360–1079 px, laptop 1080–1365 px, desktop ≥ 1366 px
- Config hook: pass
settingswith{ min, max, enable }overrides per device band - DOM effect: toggles
displayinline on[string-mobile|tablet|laptop|desktop]elements - Event bus: emits
screen:mobile|tablet|laptop|desktopwith a boolean payload on state changes - Lifecycle: re-evaluates on init and every resize, keeping MatchMedia instances in sync with updated ranges
Basic Usage
import StringTune, { StringResponsive } from '@fiddle-digital/string-tune';
const stringTune = StringTune.getInstance();
stringTune.use(StringResponsive, {
settings: {
mobile: { max: 767 },
tablet: { min: 768, max: 1023 },
desktop: { min: 1440 },
},
});
stringTune.start(60);
stringTune.on('screen:desktop', (active) => {
document.body.classList.toggle('is-desktop', active);
});
<header string-mobile>Compact nav</header>
<header string-tablet string-desktop>Wide nav with search</header>
<aside string-laptop string-desktop>
<nav>Sidebar only when there is room</nav>
</aside>
[string-mobile],
[string-tablet],
[string-laptop],
[string-desktop] {
transition: opacity 0.2s ease;
}
[style*='display: none'] {
opacity: 0;
}
How It Works
- Query preparation: four
StringResponsiveQueryDevicehelpers holdmin,max, andenableflags. Defaults cover mobile → desktop in ascending widths. - Runtime override: when you pass
settings, the module rewires each helper, rebuilds itswindow.matchMediainstance, and optionally disables a band by omitting it. - State comparison: on init and every resize it evaluates each query; if a band’s
matchesflag changes it emits the correspondingscreen:*boolean and updates internal state caches to avoid duplicate events. - DOM filtering: the module scans for elements carrying any
string-*breakpoint attribute, determines if at least one of their tags is currently active, and setsdisplayaccordingly. - Multiple tags: elements can declare more than one attribute (e.g.
string-tablet string-desktop); a single active match keeps them visible.
Event Signals
| Channel | Payload | Fired when | Use cases |
|---|---|---|---|
screen:mobile | boolean | Mobile query toggles state | Toggle mobile menus, activate touch-first tweaks |
screen:tablet | boolean | Tablet query toggles state | Adjust typography, hydrate tablet-only components |
screen:laptop | boolean | Laptop query toggles state | Load medium-resolution assets, switch layout columns |
screen:desktop | boolean | Desktop query toggles state | Enable heavy effects, persistent sidebars, analytics |
StringResponsive keeps markup-driven breakpoints expressive—tag the DOM once, stream range changes through the event bus, and delegate width-specific logic without wiring custom media-query listeners by hand.