Skip to main content
Introducing packages.sweber.dev
Documentation menuLagrangian Pro API

Lagrangian Pro API

Every function, option and method of the framework-free Lagrangian Pro package, generated from its type declarations.

import { sheet, dialog, board /* … */ } from "@weber-development/lagrangian-pro";

From 1.0 these signatures follow semantic versioning: options and methods are only removed in a major version, and deprecated ones are listed in the changelog first.

board

board(target: HTMLElement | readonly HTMLElement[], options?: BoardOptions): Board
OptionType
handlestringSelector of the drag handle inside each item. Default the whole item.
liftnumberScale of the lifted item. Default 1.03.
springSpringOptions
autoScrollboolean | AutoScrollOptionsScroll the page or the scrollable parent while an item is held near its edge. Default true; { edge, speed } tunes it, false turns it off.
canDrop(item: HTMLElement, container: HTMLElement) => booleanDecides whether an item may be dropped into a container. Default always.
onMove(change: BoardChange) => voidCalled after an item has been dropped in a new place.
commit(change: BoardChange) => void | Promise<void>Changes the order yourself, e.g. through framework state. The DOM is put back as it was, then this is called with the change; the transition plays once the returned promise resolves, so resolve it after the DOM is updated.
messagesPartial<BoardMessages>Screen reader announcements, e.g. translated. English by default.
MemberType
containersHTMLElement[]
draggingboolean
move(item: HTMLElement, container: HTMLElement, index: number): voidMoves an item to a container and index with a spring transition.
destroy(): void
carousel(viewport: HTMLElement, options?: CarouselOptions): Carousel
OptionType
trackHTMLElementThe element that moves. Default the first child of the viewport; its children are the slides.
align"start" | "center"Where the active slide rests in the viewport. Default "start".
initialnumberSlide to start at. Default 0.
flicknumberSpeed in px/s from which a flick moves at least one slide. Default 350.
springSpringOptionsSpring for settling.
labelstringLabel read by screen readers.
onChange(index: number) => voidCalled when a new slide becomes the active one.
MemberType
indexnumber
countnumber
xPhysicsValue
draggingboolean
goTo(index: number): Promise<boolean>
next(): Promise<boolean>
prev(): Promise<boolean>
refresh(): voidMeasures the slides again, e.g. after a resize or after adding slides.
destroy(): void

dialog

dialog(element: HTMLElement, options?: DialogOptions): Dialog
OptionType
backdropHTMLElementElement behind the dialog that fades with it; a click on it closes the dialog.
initialFocusHTMLElement | stringElement to focus when opening. Default the first focusable element.
closeOnEscapebooleanClose with Escape. Default true.
closeOnBackdropbooleanClose with a click on the backdrop. Default true.
lockScrollbooleanStop the page behind from scrolling while open. Default true.
fromnumberScale the dialog starts from. Default 0.92.
springSpringOptionsSpring for the transition.
onChange(open: boolean) => void
MemberType
openboolean
show(): Promise<boolean>
hide(): Promise<boolean>
destroy(): void

drawer

drawer(element: HTMLElement, options?: DrawerOptions): Drawer
OptionType
side"left" | "right"Edge the drawer sits at. Default "left".
openbooleanStart open. Default false.
handleHTMLElementThe part that can be dragged. Default the whole drawer.
backdropHTMLElementAn element whose opacity follows the drawer; a click on it closes the drawer.
closeOnEscapebooleanClose with the Escape key while open. Default true.
flicknumberSpeed in px/s from which a flick opens or closes. Default 400.
springSpringOptionsSpring for opening and closing.
onChange(open: boolean) => voidCalled when the drawer settles open or closed.
MemberType
xPhysicsValueHorizontal translation: 0 is open, the width (negative on the left) is closed.
openboolean
draggingboolean
show(): Promise<boolean>
hide(): Promise<boolean>
toggle(): Promise<boolean>
destroy(): void

fall

fall(root: HTMLElement, options?: FallOptions): Fall
OptionType
selectorstringElements that fall. Default the children of root.
boundsRectFloor and walls in viewport pixels. Default the viewport.
gravitynumberGravity in m/s². Default 9.81.
restitutionnumberBounciness of the pieces, 0 to 1. Default 0.3.
staggernumberMilliseconds between two pieces letting go, from the bottom up. Default 40.
springSpringOptionsSpring used to bring the pieces home.
MemberType
activebooleanTrue from start() until restore() has finished.
worldWorldThe world the pieces live in, e.g. to add more bodies.
start(): voidLets the pieces go.
explode(origin?: { x: number; y: number; }, strength?: number): voidThrows the pieces away from a point in viewport pixels. strength is a speed in px/s.
restore(): Promise<void>Springs every piece back into the layout.
destroy(): void

measure

measure(targets: Targets): Snapshot
MemberType
play(spring?: SpringOptions): voidSprings every measured element from where it was to where it is now.

indicator

indicator(track: HTMLElement, element: HTMLElement, options?: IndicatorOptions): Indicator
OptionType
itemSelectorstringCSS selector of the items inside the track. Default the track's children.
initialnumber | HTMLElementItem selected at the start. Default the one with aria-selected="true", else the first.
clickToSelectbooleanSelect an item when it is clicked. Default true.
springSpringOptions
onSelect(index: number, item: HTMLElement) => void
MemberType
indexnumber
select(target: number | HTMLElement): void
refresh(): voidMoves to the selected item without animation, e.g. after a resize.
destroy(): void

jelly

jelly(skin: HTMLElement | SVGElement, options?: JellyOptions): Jelly
OptionType
sourceJellySourceWhat moves: a draggable, a pair of values, an element animated with Lagrangian, or a function returning the velocity in px/s (e.g. of a World body). Default the parent element.
strengthnumberStretch per 1000 px/s. Default 0.1.
maxnumberLargest stretch, e.g. 0.3 for 30 %. Default 0.3.
frequencynumberWobble frequency in Hz. Default 4.5.
dampingnumberDamping ratio of the wobble, 0 to 1. Lower wobbles longer. Default 0.3.
MemberType
stretchnumberCurrent stretch along the main axis, e.g. 0.12 for 12 %; negative is squashed.
impact(vx: number, vy: number): voidSquashes the shape as after a hit with this velocity in px/s, e.g. from onCollide.
destroy(): void

popover

popover(trigger: HTMLElement, element: HTMLElement, options?: PopoverOptions): Popover
OptionType
sidePopoverSideSide of the trigger the popover opens on. Default "bottom".
alignPopoverAlignAlignment along that side. Default "center".
offsetnumberDistance to the trigger in pixels. Default 8.
marginnumberKeep this far from the edge of the window in pixels. Default 8.
flipbooleanOpen on the other side when there is no room. Default true.
closeOnEscapebooleanClose with Escape. Default true.
closeOnOutsidebooleanClose with a click outside of the popover and the trigger. Default true.
focusbooleanMove the focus into the popover when opening. Default true.
fromnumberScale the popover starts from. Default 0.9.
springSpringOptions
onChange(open: boolean) => void
MemberType
openboolean
sidePopoverSideThe side it is currently on, after flipping.
show(): Promise<boolean>
hide(): Promise<boolean>
toggle(): Promise<boolean>
update(): voidPositions it again, e.g. after its content changed size.
destroy(): void

pullToRefresh

pullToRefresh(container: HTMLElement, options: PullToRefreshOptions): PullToRefresh
OptionType
onRefresh() => void | Promise<void>Called on release past the threshold. The content waits until the promise settles.
contentHTMLElementThe element that moves. Default the first child of the scroll container.
thresholdnumberPull distance in pixels that triggers a refresh. Default 72.
maxnumberLargest visible pull in pixels. Default 2.2 times the threshold.
springSpringOptionsSpring for holding and returning.
onProgress(progress: number, state: PullState) => voidCalled on every move with the pull as a fraction of the threshold (0 to 1+).
onState(state: PullState) => voidCalled when the state changes; also written to data-state on the container.
MemberType
statePullState
refresh(): Promise<void>Runs a refresh from code, e.g. for a keyboard button. Resolves when the content is back.
destroy(): void

rope

rope(options: RopeOptions): Rope
OptionType
fromVectorFirst point, pinned. In pixels.
toVectorLast point. Pinned when pinEnd is true, otherwise only its starting place.
segmentsnumberNumber of segments. More is smoother and slower. Default 20.
lengthnumberRest length in pixels. Default 1.1 × the distance between from and to, or 200.
pinEndbooleanPin the last point as well, e.g. for a garland. Default false.
gravityVectorGravity in px/s². Default { x: 0, y: 2000 }.
dragnumberFraction of velocity lost per second to air and internal friction. Default 0.8.
iterationsnumberConstraint passes per step. More is less stretchy. Default 20.
onFrame(rope: Rope) => voidCalled after every frame, e.g. to draw.

sheet

sheet(element: HTMLElement, options?: SheetOptions): Sheet
OptionType
detentsnumber[]Visible heights the sheet rests at, lowest first: pixels, or fractions of the sheet's height for values up to 1. Default [0.5, 1].
initialnumberDetent to start at, or -1 for closed. Default -1 when dismissible, otherwise 0.
dismissiblebooleanWhether a drag or throw downwards can close the sheet. Default true.
handleHTMLElementThe part of the sheet that can be dragged. Default the whole sheet.
backdropHTMLElementAn element whose opacity follows the sheet, from 0 (closed) to 1 (at the first detent).
closeOnEscapebooleanClose with the Escape key while open. Default true when dismissible.
flicknumberSpeed in px/s from which a flick moves at least one detent. Default 400.
springSpringOptionsSpring for settling on a detent. Default a quick spring with a hint of bounce.
onChange(index: number) => voidCalled when the sheet settles on a new detent index, -1 when it closes.
MemberType
yPhysicsValueThe vertical translation in pixels: 0 is fully open, the sheet height is closed.
indexnumberCurrent detent index, -1 when closed.
draggingboolean
snapTo(index: number): Promise<boolean>Springs to a detent, or closes with -1. Keeps the current velocity.
open(index?: number): Promise<boolean>Opens at the given detent, default the first.
close(): Promise<boolean>
destroy(): void

sortable

sortable(list: HTMLElement, options?: SortableOptions): Sortable
OptionType
axis"x" | "y"Direction of the list. Default "y".
handlestringSelector of the drag handle inside each item. Default the whole item.
liftnumberScale of the lifted item. Default 1.03.
springSpringOptionsSpring for making room and settling.
messagesPartial<SortableMessages>Screen reader announcements, e.g. translated. English by default.
autoScrollboolean | AutoScrollOptionsScroll the page or the scrollable parent while an item is held near its edge. Default true; { edge, speed } tunes it, false turns it off.
onReorder(from: number, to: number, items: HTMLElement[]) => voidCalled after the order changed, with the old and new index and the items in order.
reorder(from: number, to: number) => void | Promise<void>Changes the order yourself instead of moving the DOM nodes, e.g. through framework state. Called for every single move; the transition plays once the returned promise resolves (or right away when nothing is returned), so resolve it after the DOM is updated.
MemberType
itemsHTMLElement[]The items in their current order.
draggingboolean
move(from: number, to: number): voidMoves an item to a new index with a spring transition.
destroy(): void

impactSound

impactSound(options?: ImpactSoundOptions): ImpactSound
OptionType
volumenumberMaster volume, 0 to 1. Default 0.4.
materialMaterialTimbre. Default "wood".
minSpeednumberImpacts slower than this in px/s are silent. Default 120.
maxSpeednumberImpact speed in px/s that plays at full volume. Default 2500.
pitchnumberPitch multiplier. Default 1.
voicesnumberSounds playing at once. Default 6.
contextAudioContextAn existing audio context. Default a new one, created on first use.
MemberType
mutedbooleanSilences all sounds without destroying anything.
volumenumber
materialMaterial
play(strength: number, size?: number): booleanPlays one impact. strength 0 to 1, size in pixels (a radius; default 24). Returns false when it was skipped (muted, too quiet, no audio, or all voices busy).
collide(a: Body, b: Body | null, speed: number) => voidUse as a World's onCollide: new World({ onCollide: sound.collide }).
unlock(): Promise<void>Resumes audio after a user gesture. Called automatically on the first pointer or key press.
destroy(): void

swipeStack

swipeStack(container: HTMLElement, options?: SwipeStackOptions): SwipeStack
OptionType
directionsSwipeDirection[]Directions a card can leave in. Default left and right.
thresholdnumberDistance as a fraction of the card size after which a release swipes. Default 0.35.
velocitynumberThrow speed in px/s that swipes regardless of the distance. Default 650.
rotatenumberTilt in degrees at the threshold. Default 12.
visiblenumberCards behind the top one that stay visible. Default 2.
depth{ scale?: number; y?: number; }How much smaller and lower each card behind is. Default { scale: 0.05, y: 14 }.
springSpringOptionsSpring for returning and moving up.
onMove(direction: SwipeDirection | null, progress: number, card: HTMLElement) => voidCalled while dragging with the leading direction and progress to the threshold (0 to 1).
onSwipe(card: HTMLElement, direction: SwipeDirection, index: number) => voidCalled when a card leaves.
onEmpty() => voidCalled when the last card has left.
MemberType
topHTMLElement | nullThe card on top, or null when all are gone.
remainingnumberCards left on the stack.
swipe(direction: SwipeDirection): Promise<boolean>Swipes the top card away, as if thrown. Resolves when it is off screen.
undo(): booleanBrings the last swiped card back onto the stack. Returns false when there is none.
refresh(): voidReads the cards from the container again, e.g. after adding new ones at the end.
destroy(): void

toasts

toasts(container: HTMLElement, options?: ToastOptions): Toasts
OptionType
edge"top" | "bottom"Where the stack sits; new toasts arrive from this edge. Default "bottom".
maxnumberToasts shown at once; older ones are dismissed. Default 4.
durationnumberMilliseconds until a toast closes itself. 0 keeps it. Default 5000.
swipebooleanSwipe sideways to dismiss. Default true.
distancenumberDistance in pixels a toast travels when it arrives. Default 32.
springSpringOptions
MemberType
show(content: string | Node, options?: ShowOptions): ToastHandle
clear(): Promise<void>Dismisses every toast.
countnumber
destroy(): void

zoom

zoom(viewport: HTMLElement, options?: ZoomOptions): Zoom
OptionType
contentHTMLElementThe element that is zoomed. Default the first child of the viewport.
minnumberSmallest and largest scale. Default 1 and 6.
maxnumber
doubleTapnumber | falseScale a double tap zooms to. Default 2.5; false turns the gesture off.
wheelbooleanZoom with the mouse wheel and trackpad pinch. Default true.
keysbooleanKeys: + and - zoom, 0 resets, arrows pan, while the viewport has focus. Default true.
rubberbandnumberResistance past the edges, 0 to 1. Default 0.55.
springSpringOptions
onChange(state: ZoomState) => void
MemberType
stateZoomState
scalePhysicsValue
xPhysicsValue
yPhysicsValue
zoomTo(scale: number, around?: { x: number; y: number; }, spring?: SpringOptions): voidZooms to a scale around a point in viewport pixels (default the centre).
panTo(point: { x: number; y: number; }, spring?: SpringOptions): voidMoves the content so the point (in content pixels) is at the centre of the viewport.
reset(spring?: SpringOptions): void
refresh(): voidMeasures again after the sizes changed.
destroy(): void