UNPKG

lisn.js

Version:

Simply handle user gestures and actions. Includes widgets.

1 lines 9.72 kB
{"version":3,"file":"types.cjs","names":[],"sources":["../../../src/ts/globals/types.ts"],"sourcesContent":["/**\n * @module Types\n */\n\nimport { settings } from \"@lisn/globals/settings\";\n\n/**\n * @category Layout\n */\nexport type Device = keyof typeof settings.deviceBreakpoints;\n\n/**\n * @category Layout\n */\nexport type AspectRatio = keyof typeof settings.aspectRatioBreakpoints;\n\n/**\n * @category Layout\n */\nexport type Layout = Device | AspectRatio;\n\n/**\n * @category Layout\n */\nexport type LayoutSpec<L extends Layout> =\n | CommaSeparatedStr<L>\n | `max ${L}`\n | `min ${L}`\n | RangeStr<\" to \", L>;\n\n/**\n * @category Layout\n */\nexport type DeviceSpec = LayoutSpec<Device>;\n\n/**\n * @category Layout\n */\nexport type AspectRatioSpec = LayoutSpec<AspectRatio>;\n\n/**\n * @category Layout\n */\nexport type Offset = \"top\" | \"bottom\" | \"left\" | \"right\";\n\n/**\n * @category Layout\n */\nexport type Position = Offset;\n\n/**\n * @category Layout\n */\nexport type FlexDirection = \"row\" | \"column\" | \"row-reverse\" | \"column-reverse\";\n\n/**\n * @category Views\n */\nexport type ViewTarget = Element | ScrollOffset;\n\n/**\n * The {@link Watchers/ViewWatcher.ViewWatcherConfig.root | root}'s\n * (default is the viewport) view relative to the target:\n *\n * - \"above\": the root is above the target, i.e. target is below the root\n * - \"below\": the root is below the target, i.e. target is above the root\n * - \"left\": the root is to the left the target, i.e. target is on the right\n * of the root\n * - \"right\": the root is to the right the target, i.e. target is on the left\n * of the root\n * - \"at\": the target is in view of the root\n *\n * @category Views\n */\nexport type View = \"at\" | \"above\" | \"below\" | \"left\" | \"right\";\n\n/**\n * This has the format of <ref>: <value> where <value> can be any valid CSS\n * offset specification*.\n *\n * <ref> must be one of top, bottom, left or right.\n *\n * Using left/right reference only makes sense if the document scrolls\n * horizontally, which is rare. In most cases you would want to use top/bottom\n * as reference.\n *\n * * See for example {@link https://developer.mozilla.org/en-US/docs/Web/CSS/top}\n *\n * @category Views\n */\nexport type ScrollOffset = `${Offset}: ${string}`;\n\n/**\n * @category Sizing\n */\nexport type SizeTarget = Element | Document | (Window & typeof globalThis);\n\n/**\n * @category Sizing\n */\nexport type Box = \"content\" | \"border\";\n\n/**\n * @category Sizing\n */\nexport type Dimension = \"width\" | \"height\";\n\n/**\n * @category Sizing\n */\nexport type Size = { width: number; height: number };\n\n/**\n * - added: The target was added to the tree\n * - removed: The target was removed from the tree\n * - attribute: An attribute of the target changed\n *\n * @category DOM\n */\nexport type MutationCategory = \"added\" | \"removed\" | \"attribute\";\n\n/**\n * @category Scrolling\n */\nexport type ScrollTarget = Element | Document | (Window & typeof globalThis);\n\n/**\n * If a given coordinate is a function, it is called at the moment the\n * scrolling action begins (and therefore the up-to-date value of the\n * coordinate is needed), and is passed a single argument: the scrolling\n * element that is being scrolled.\n *\n * @category Scrolling\n */\nexport type TargetCoordinate = number | ((scrollable: Element) => number);\n\n/**\n * @category Scrolling\n */\nexport type TargetCoordinates = AtLeastOne<{\n top: TargetCoordinate;\n left: TargetCoordinate;\n}>;\n\n/**\n * @category Scrolling\n */\nexport type CoordinateOffset = AtLeastOne<{\n top: number;\n left: number;\n}>;\n\n/**\n * @category Scrolling\n */\nexport type ScrollPosition = {\n top: number;\n left: number;\n};\n\n/**\n * @category Directions\n */\nexport type XYDirection = \"up\" | \"down\" | \"left\" | \"right\";\n\n/**\n * @category Directions\n */\nexport type ZDirection = \"in\" | \"out\";\n\n/**\n * @category Directions\n */\nexport type NoDirection = \"none\";\n\n/**\n * @category Directions\n */\nexport type AmbiguousDirection = \"ambiguous\";\n\n/**\n * @category Directions\n */\nexport type ScrollDirection = XYDirection | NoDirection | AmbiguousDirection;\n\n/**\n * @category Directions\n */\nexport type Direction =\n | XYDirection\n | ZDirection\n | NoDirection\n | AmbiguousDirection;\n\n/**\n * Indicates the device used:\n * - \"key\": results from key presses, in particular Up/Down/Left/Right/PageUp/PageDown/Home/End/Space\n * - \"pointer\": results from dragging with the mouse\n * - \"touch\": results from touch\n * - \"wheel\": results from mouse wheel or trackpad\n *\n * @category Gestures\n */\nexport type GestureDevice = \"key\" | \"pointer\" | \"touch\" | \"wheel\";\n\n/**\n * Indicates the likely intent of the gesture, or the likely result of the\n * action if it is not prevented. Note that this cannot be 100% reliable as it\n * depends on the browser and the target element.\n * - \"scroll\": Would likely result in a scroll or a pan (for touch events\n * involving multiple fingers). Results from mouse or trackpad\n * wheel events, from touch moves involving any number of fingers,\n * or from directional key presses (Arrows, Home, etc), unless\n * those actions are deemed to be zoom actions (see below).\n * - \"zoom\": Would likely result in a zoom. Results from mouse wheel while\n * holding Ctrl key, from trackpad or touch screen pinch motion or\n * from pressing +/- on keyboard.\n * - \"drag\": Would likely result in a drag. Results from pressing the\n * primary mouse button then moving.\n *\n * @category Gestures\n */\nexport type GestureIntent = \"scroll\" | \"zoom\" | \"drag\" | \"unknown\";\n\n/**\n * Screen coordinate. 0, 0 is top-left corner.\n *\n * @category Misc\n */\nexport type Point = [/** x position */ number, /** y position */ number];\n\n/**\n * Represents a change of position. Positive x is to the right, positive y is down.\n *\n * @category Misc\n */\nexport type Vector = [\n /** deltaX/x component */ number,\n /** deltaY/y component */ number,\n];\n\n/**\n * @category Misc\n */\nexport type PointerAction = \"click\" | \"hover\" | \"press\";\n\n/**\n * @category Misc\n */\nexport type BoundingRect = {\n x: number;\n left: number;\n right: number;\n width: number;\n y: number;\n top: number;\n bottom: number;\n height: number;\n};\n\n/**\n * @param args Arguments to log\n *\n * @category Misc\n */\nexport type LogFunction = (...args: unknown[]) => void;\n\n/**\n * @category Utility\n */\nexport type DOMElement = HTMLElement | SVGElement | MathMLElement;\n\n/**\n * @category Utility\n */\nexport type IterableObject<T> = Iterable<T> & object;\n\n/**\n * @category Utility\n */\nexport type OnlyOneOf<T, U> =\n | ({\n [P in keyof T]: T[P];\n } & {\n [P in keyof U]?: never;\n })\n | ({\n [P in keyof U]: U[P];\n } & {\n [P in keyof T]?: never;\n });\n\n/**\n * @category Utility\n */\nexport type AtLeastOne<T, U = { [K in keyof T]: Pick<T, K> }> = Partial<T> &\n U[keyof U];\n\n/**\n * @category Utility\n */\nexport type NonEmptyArray<T> = [T, ...T[]];\n\n/**\n * @category Utility\n */\nexport type NonNullableReturnType<F extends (...args: unknown[]) => unknown> =\n NonNullable<ReturnType<F>>;\n\n/**\n * @category Utility\n */\nexport type StrRecord =\n | {\n [key: string]: string | number | boolean | null | StrRecord;\n }\n | Array<string | number | boolean>;\n\n/**\n * @category Utility\n */\nexport type SeparatedStr<\n S extends string,\n T extends string,\n Key extends T = T,\n> = Key extends string\n ? `${Key},${SeparatedStr<S, Exclude<T, Key>>}` | Key\n : never;\n\n/**\n * @category Utility\n */\nexport type CommaSeparatedStr<\n T extends string,\n Key extends T = T,\n> = SeparatedStr<\",\", T, Key>;\n\n/**\n * @category Utility\n */\nexport type RangeStr<\n S extends string,\n T extends string,\n Key extends T = T,\n> = Key extends string ? `${Key}${S}${Exclude<T, Key>}` : never;\n\n/**\n * @category Utility\n */\n/* eslint-disable-next-line @typescript-eslint/no-explicit-any */\nexport type Constructor<T> = new (...args: any[]) => T;\n\n/**\n * @category Utility\n */\nexport type InstanceType<C> = C extends Constructor<infer T> ? T : never;\n\n/**\n * @category Utility\n */\nexport type MapBase<K, V> = {\n get: (key: K) => V | undefined;\n set: (key: K, value: V) => void;\n delete: (key: K) => void;\n has: (key: K) => boolean;\n};\n\n/**\n * @category Utility\n */\nexport type SetBase<V> = {\n add: (value: V) => SetBase<V>;\n delete: (value: V) => void;\n has: (value: V) => boolean;\n};\n\n/**\n * @ignore\n * @internal\n */\n/* eslint-disable-next-line @typescript-eslint/no-explicit-any */\nexport type Spread<A extends readonly [...any]> = A extends [\n infer L,\n ...infer R,\n]\n ? SpreadTwo<L, Spread<R>>\n : unknown;\n\n// --------------------\n\ntype OptionalPropertyNames<T> = {\n /* eslint-disable-next-line @typescript-eslint/no-empty-object-type */\n [K in keyof T]-?: {} extends { [P in K]: T[K] } ? K : never;\n}[keyof T];\n\ntype SpreadProperties<L, R, K extends keyof L & keyof R> = {\n [P in K]: L[P] | Exclude<R[P], undefined>;\n};\n\ntype Id<T> = T extends infer U ? { [K in keyof U]: U[K] } : never;\n\ntype SpreadTwo<L, R> = Id<\n Pick<L, Exclude<keyof L, keyof R>> &\n Pick<R, Exclude<keyof R, OptionalPropertyNames<R>>> &\n Pick<R, Exclude<OptionalPropertyNames<R>, keyof L>> &\n SpreadProperties<L, R, OptionalPropertyNames<R> & keyof L>\n>;\n"],"mappings":"","ignoreList":[]}