Utils
@agencecinq/utils is the shared contract between CINQ UI packages: event
names, a small dispatchEvent helper, focus/scroll utilities, and attribute
parsers. Every component doc assumes these conventions. This page is the single
reference when you wire events or reuse overlay helpers in your own code.
Install
Section titled “Install”pnpm add @agencecinq/utilsEvery component package depends on @agencecinq/utils and imports it, so a page
built with a bundler loads one copy. Import from the package directly when you
listen to events or call helpers:
import { EVENTS, dispatchEvent, disableScroll, enableScroll } from "@agencecinq/utils";A page without a bundler maps the package in its import map, next to the components:
<script type="importmap"> { "imports": { "@agencecinq/utils": "/assets/utils.js" } }</script>Event naming
Section titled “Event naming”Custom events follow {package}:{action} in kebab-case:
drawer:before-open,tabs:activate,accordion:open- Constants live on
EVENTS(e.g.EVENTS.DRAWER_BEFORE_OPENto"drawer:before-open")
Prefer EVENTS.* over string literals so renames stay centralized.
Where to listen
Section titled “Where to listen”| Target | Packages | Notes |
|---|---|---|
document.documentElement |
Drawer, Modal, Sheet | Before-hooks (before-open, before-close), with bubbles: false. The dialog’s native toggle follows the state. |
| Host custom element | Spinbutton, Switch, Combobox, Window Splitter, Calendar | Listen on the <cinq-*> instance. |
| Panel / tab child (bubbles) | Accordion, Tabs | Fired on [data-panel] or [role="tab"]. Listen on <cinq-accordion> / <cinq-tabs> or the child. |
| Trigger button | Disclosure Button | Fired on the slotted <button>. |
Cancelable hooks
Section titled “Cancelable hooks”Drawer, Modal and Sheet dispatch a before event with
dispatchBeforeEvent: preventDefault() cancels the
action, detail.waitUntil(promise) defers it. Accordion, Tabs and Disclosure
Button rely on preventDefault() only: when dispatchEvent returns false,
the component aborts the state change.
document.documentElement.addEventListener(EVENTS.DRAWER_BEFORE_OPEN, (event) => { event.detail.waitUntil(loadBestiary());});EVENTS catalog
Section titled “EVENTS catalog”Grouped by package. See each component page for payload shapes and examples.
| Constant | Event | Cancelable | Component |
|---|---|---|---|
DRAWER_BEFORE_OPEN |
drawer:before-open |
Yes | Drawer |
DRAWER_BEFORE_CLOSE |
drawer:before-close |
Yes | Drawer |
SHEET_BEFORE_OPEN |
sheet:before-open |
Yes | Sheet |
SHEET_BEFORE_CLOSE |
sheet:before-close |
Yes | Sheet |
MODAL_BEFORE_OPEN |
modal:before-open |
Yes | Modal |
MODAL_BEFORE_CLOSE |
modal:before-close |
Yes | Modal |
TABS_BEFORE_ACTIVATE |
tabs:before-activate |
Yes | Tabs |
TABS_ACTIVATE |
tabs:activate |
No | Tabs |
TABS_DELETE |
tabs:delete |
No | Tabs |
ACCORDION_OPEN |
accordion:open |
Yes | Accordion |
ACCORDION_CLOSE |
accordion:close |
Yes | Accordion |
DISCLOSURE_BUTTON_OPEN |
disclosure-button:open |
Yes | Disclosure Button |
DISCLOSURE_BUTTON_CLOSE |
disclosure-button:close |
Yes | Disclosure Button |
DROPZONE_CHANGE |
dropzone:change |
No | Dropzone |
DROPZONE_REJECT |
dropzone:reject |
No | Dropzone |
SWITCH_ACTIVATE |
switch:activate |
Yes | Switch |
SWITCH_DEACTIVATE |
switch:deactivate |
Yes | Switch |
TOAST_OPEN |
toast:open |
No | Toast |
TOAST_CLOSE |
toast:close |
No | Toast |
SPINBUTTON_CHANGE |
spinbutton:change |
No | Spinbutton |
COMBOBOX_LOADING |
combobox:loading |
No | Combobox |
COMBOBOX_LOADED |
combobox:loaded |
No | Combobox |
COMBOBOX_UPDATE |
combobox:update |
No | Combobox |
COMBOBOX_SUBMIT |
combobox:submit |
No | Combobox |
COMBOBOX_EMPTY |
combobox:empty |
No | Combobox |
WINDOWSPLITTER_CHANGE |
windowsplitter:change |
No | Window Splitter |
SLIDER_CHANGE |
slider:change |
No | Slider |
SNAKE_EAT |
snake:eat |
No | Snake |
SNAKE_OVER |
snake:over |
No | Snake |
SNAKE_REPLAY |
snake:replay |
No | Snake |
CALENDAR_RENDER |
calendar:render |
No | Calendar |
CALENDAR_BEFORE_CHANGE |
calendar:before-change |
Yes | Calendar |
CALENDAR_CHANGE |
calendar:change |
No | Calendar |
CART_BEFORE_ADD |
cart:before-add |
Yes | Host apps (Shopify cart) |
CART_BEFORE_UPDATE |
cart:before-update |
Yes | Host apps (Shopify cart) |
CART_UPDATE |
cart:update |
No | Host apps (Shopify cart) |
VARIANT_CHANGE |
variant:change |
No | Host apps (product variants) |
dispatchEvent
Section titled “dispatchEvent”import { EVENTS, dispatchEvent } from "@agencecinq/utils";
const proceed = dispatchEvent(host, EVENTS.ACCORDION_OPEN, { el, index });if (!proceed) return; // a listener called preventDefault()Defaults: bubbles: true, cancelable: true. Notifications such as
toast:open pass { cancelable: false }.
Returns false when a listener cancels, otherwise true.
dispatchBeforeEvent
Section titled “dispatchBeforeEvent”import { EVENTS, dispatchBeforeEvent } from "@agencecinq/utils";
const result = dispatchBeforeEvent(document.documentElement, EVENTS.DRAWER_BEFORE_OPEN, { drawer: id,});Dispatches a cancelable, non-bubbling event whose detail gets a
waitUntil(promise) method. Returns a boolean when no listener deferred, so
the caller can commit synchronously, otherwise a promise. The action proceeds
once every deferred promise fulfills, and is canceled by preventDefault() or a
rejected promise. waitUntil() throws if called after dispatch. Types:
WaitUntil, BeforeEventDetail<T>.
trackDrag(element, options) follows one pointer gesture at a time: it captures
the pointer, follows that pointer only, sets [dragging] on a host, and hands
back the distance and speed of the drag. The Drag page has the
options, the values and a live example.
import { trackDrag } from "@agencecinq/utils";
const drag = trackDrag($handle, { host: this, move: (event, { dx, dy }) => {}, end: (event, { dx, dy, vx, vy, canceled }) => {},});
drag.destroy();Focus helpers
Section titled “Focus helpers”A modal <dialog> keeps Tab inside itself and gives the focus back to the
control that opened it, so utils has no focus trap.
| Export | Role |
|---|---|
getFocusableElements(container) |
Visible, tabbable descendants. |
focusByKey(event, elements, index) |
Move focus between elements with the arrow, Home and End keys. Returns the new index, or undefined for a key left to the browser. |
focusByKey() is the keyboard of the accordion, the tabs and the disclosure
navigation. It wraps at both ends, swaps left and right in a right-to-left context
(read with :dir(rtl)), cancels the default action of the keys it handles, and
leaves the keys pressed with Alt, Ctrl or Meta to the browser.
Scroll lock
Section titled “Scroll lock”| Export | Role |
|---|---|
disableScroll() |
Lock document scroll and preserve position for restore. |
enableScroll(position?) |
Unlock and resume previous Y unless position is a number or false. |
Use them for full-screen overlays that do not lock scroll in CSS.
Attribute parsers
Section titled “Attribute parsers”| Export | Role |
|---|---|
parseBoolean(value, fallback?) |
"false" / "0" to false, absent to fallback. |
parseNumber(value, fallback) |
Finite number or fallback. |
parseList(value) |
Space-separated ID tokens to string[]. |
Other exports
Section titled “Other exports”| Export | Role |
|---|---|
clamp(value, min, max) |
Numeric clamp. |
throttle(fn, wait) |
Trailing throttle (used by Spinbutton change events). |
debounce(fn, wait) |
Trailing debounce (fire once after calls stop). |