All files / globals types.ts

0% Statements 0/1
100% Branches 0/0
100% Functions 0/0
0% Lines 0/1

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
/**
 * @module Types
 */
 
import { settings } from "@lisn/globals/settings";
 
/**
 * @category Layout
 */
export type Device = keyof typeof settings.deviceBreakpoints;
 
/**
 * @category Layout
 */
export type AspectRatio = keyof typeof settings.aspectRatioBreakpoints;
 
/**
 * @category Layout
 */
export type Layout = Device | AspectRatio;
 
/**
 * @category Layout
 */
export type LayoutSpec<L extends Layout> =
  | CommaSeparatedStr<L>
  | `max ${L}`
  | `min ${L}`
  | RangeStr<" to ", L>;
 
/**
 * @category Layout
 */
export type DeviceSpec = LayoutSpec<Device>;
 
/**
 * @category Layout
 */
export type AspectRatioSpec = LayoutSpec<AspectRatio>;
 
/**
 * @category Layout
 *
 * @since v1.2.1 Previously called `Offset`
 */
export type Anchor = "top" | "bottom" | "left" | "right";
 
/**
 * @ignore
 * @deprecated
 *
 * Deprecated alias for {@link Anchor}
 */
export type Offset = Anchor;
 
/**
 * @category Layout
 */
export type Position = Anchor;
 
/**
 * @category Layout
 */
export type FlexDirection = "row" | "column" | "row-reverse" | "column-reverse";
 
/**
 * @category Views
 */
export type ViewTarget = Element | ScrollOffsetSpec;
 
/**
 * The {@link Watchers/ViewWatcher.ViewWatcherConfig.root | root}'s
 * (default is the viewport) view relative to the target:
 *
 * - "above": the root is above the target, i.e. target is below the root
 * - "below": the root is below the target, i.e. target is above the root
 * - "left":  the root is to the left the target, i.e. target is on the right
 *            of the root
 * - "right": the root is to the right the target, i.e. target is on the left
 *            of the root
 * - "at":    the target is in view of the root
 *
 * @category Views
 */
export type View = "at" | "above" | "below" | "left" | "right";
 
/**
 * This has the format of <ref>: <value> where <value> can be any valid CSS
 * offset specification*.
 *
 * <ref> must be one of top, bottom, left or right.
 *
 * Using left/right reference only makes sense if the document scrolls
 * horizontally, which is rare. In most cases you would want to use top/bottom
 * as reference.
 *
 * * See for example {@link https://developer.mozilla.org/en-US/docs/Web/CSS/top}
 *
 * @category Views
 *
 * @since v1.2.1 Previously called `ScrollOffset`
 */
export type ScrollOffsetSpec = `${Anchor}: ${string}`;
 
/**
 * @ignore
 * @deprecated
 *
 * Deprecated alias for {@link ScrollOffsetSpec}
 */
export type ScrollOffset = ScrollOffsetSpec;
 
/**
 * @category Sizing
 */
export type SizeTarget = Element | Document | (Window & typeof globalThis);
 
/**
 * @category Sizing
 */
export type Box = "content" | "border";
 
/**
 * @category Sizing
 */
export type Dimension = "width" | "height";
 
/**
 * @category Sizing
 */
export type Size = { width: number; height: number };
 
/**
 * - added:     The target was added to the tree
 * - removed:   The target was removed from the tree
 * - attribute: An attribute of the target changed
 *
 * @category DOM
 */
export type MutationCategory = "added" | "removed" | "attribute";
 
/**
 * @category Scrolling
 */
export type ScrollTarget = Element | Document | (Window & typeof globalThis);
 
/**
 * If a given coordinate is a function, it is called at the moment the
 * scrolling action begins (and therefore the up-to-date value of the
 * coordinate is needed), and is passed a single argument: the scrolling
 * element that is being scrolled.
 *
 * @category Scrolling
 */
export type TargetCoordinate = number | ((scrollable: Element) => number);
 
/**
 * @category Scrolling
 */
export type TargetCoordinates = AtLeastOne<{
  top: TargetCoordinate;
  left: TargetCoordinate;
}>;
 
/**
 * @category Scrolling
 */
export type CoordinateOffset = AtLeastOne<{
  top: number;
  left: number;
}>;
 
/**
 * @category Scrolling
 */
export type ScrollPosition = {
  top: number;
  left: number;
};
 
/**
 * @category Directions
 */
export type XYDirection = "up" | "down" | "left" | "right";
 
/**
 * @category Directions
 */
export type ZDirection = "in" | "out";
 
/**
 * @category Directions
 */
export type NoDirection = "none";
 
/**
 * @category Directions
 */
export type AmbiguousDirection = "ambiguous";
 
/**
 * @category Directions
 */
export type ScrollDirection = XYDirection | NoDirection | AmbiguousDirection;
 
/**
 * @category Directions
 */
export type Direction =
  | XYDirection
  | ZDirection
  | NoDirection
  | AmbiguousDirection;
 
/**
 * Indicates the device used:
 * - "key":     results from key presses, in particular Up/Down/Left/Right/PageUp/PageDown/Home/End/Space
 * - "pointer": results from dragging with the mouse
 * - "touch":   results from touch
 * - "wheel":   results from mouse wheel or trackpad
 *
 * @category Gestures
 */
export type GestureDevice = "key" | "pointer" | "touch" | "wheel";
 
/**
 * Indicates the likely intent of the gesture, or the likely result of the
 * action if it is not prevented. Note that this cannot be 100% reliable as it
 * depends on the  browser and the target element.
 * - "scroll": Would likely result in a scroll or a pan (for touch events
 *             involving multiple fingers). Results from mouse or trackpad
 *             wheel events, from touch moves involving any number of fingers,
 *             or from directional key presses (Arrows, Home, etc), unless
 *             those actions are deemed to be zoom actions (see below).
 * - "zoom":   Would likely result in a zoom. Results from mouse wheel while
 *             holding Ctrl key, from trackpad or touch screen pinch motion or
 *             from pressing +/- on keyboard.
 * - "drag":   Would likely result in a drag. Results from pressing the
 *             primary mouse button then moving.
 *
 * @category Gestures
 */
export type GestureIntent = "scroll" | "zoom" | "drag" | "unknown";
 
/**
 * Represents an absolute or relative numerical value. If the value is a number,
 * then it is taken as the actual value. Otherwise, it should be a numerical
 * string that is optionally suffixed with `%`.
 *
 * Having a `+` or `-` prefix and/or `%` suffix can be treated differently
 * depending on the use-case. Refer to the respective use-case of to see what
 * the reference value is and how percentage values are treated.
 *
 * @example
 * This is an example use-case only.
 * - `10` is treated as `10` ignoring the reference value
 * - `+10` adds `10` to the reference value
 * - `-10` subtracts `10` from the reference value
 * - `10%` multiplies the reference by `0.1`
 * - `+10%` adds 10% of one reference (e.g. maximum value) to another reference
 *   (e.g. current value)
 * - `-10%` subtracts 10% of one reference (e.g. maximum value) to another
 *   reference (e.g. current value)
 *
 * @since v1.3.0
 *
 * @category Misc
 */
export type RawOrRelativeNumber = number | `${number}` | `${number}%`;
 
/**
 * Screen coordinate. 0, 0 is top-left corner.
 *
 * @category Misc
 */
export type Point = [/** x position */ number, /** y position */ number];
 
/**
 * Represents a change of position. Positive x is to the right, positive y is down.
 *
 * @category Misc
 */
export type Vector = [
  /** deltaX/x component */ number,
  /** deltaY/y component */ number,
];
 
/**
 * @since v1.3.0
 *
 * @category Misc
 */
export type Axis =
  | [/** x component */ number, /** y component */ number]
  | [
      /** x component */ number,
      /** y component */ number,
      /** z component */ number,
    ];
 
/**
 * @since v1.3.0
 *
 * @category Misc
 */
export type Origin =
  | [/** x coordinate */ number, /** y coordinate */ number]
  | [
      /** x coordinate */ number,
      /** y coordinate */ number,
      /** z coordinate */ number,
    ];
 
/**
 * @category Misc
 */
export type PointerAction = "click" | "hover" | "press";
 
/**
 * @category Misc
 */
export type BoundingRect = {
  x: number;
  left: number;
  right: number;
  width: number;
  y: number;
  top: number;
  bottom: number;
  height: number;
};
 
/**
 * @param args Arguments to log
 *
 * @category Misc
 */
export type LogFunction = (...args: unknown[]) => void;
 
/**
 * @category Utility
 */
export type DOMElement = HTMLElement | SVGElement | MathMLElement;
 
/**
 * @category Utility
 */
export type IterableObject<T> = Iterable<T> & object;
 
/**
 * @category Utility
 */
export type OnlyOneOf<T, U> =
  | ({
      [P in keyof T]: T[P];
    } & {
      [P in keyof U]?: never;
    })
  | ({
      [P in keyof U]: U[P];
    } & {
      [P in keyof T]?: never;
    });
 
/**
 * @category Utility
 */
export type OnlyOne<T, Keys extends keyof T = keyof T> = {
  [K in Keys]: { [P in K]: T[P] } & Partial<Record<Exclude<Keys, K>, never>>;
}[Keys];
 
/**
 * @category Utility
 */
export type AtLeastOne<T, U = { [K in keyof T]: Pick<T, K> }> = Partial<T> &
  U[keyof U];
 
/**
 * @category Utility
 */
export type NonEmptyArray<T> = [T, ...T[]];
 
/**
 * @category Utility
 */
export type NonNullableReturnType<F extends (...args: unknown[]) => unknown> =
  NonNullable<ReturnType<F>>;
 
/**
 * @category Utility
 */
export type StrRecord =
  | {
      [key: string]: string | number | boolean | null | StrRecord;
    }
  | Array<string | number | boolean>;
 
/**
 * @category Utility
 */
export type SeparatedStr<
  S extends string,
  T extends string,
  Key extends T = T,
> = Key extends string
  ? `${Key},${SeparatedStr<S, Exclude<T, Key>>}` | Key
  : never;
 
/**
 * @category Utility
 */
export type CommaSeparatedStr<
  T extends string,
  Key extends T = T,
> = SeparatedStr<",", T, Key>;
 
/**
 * @category Utility
 */
export type RangeStr<
  S extends string,
  T extends string,
  Key extends T = T,
> = Key extends string ? `${Key}${S}${Exclude<T, Key>}` : never;
 
/**
 * @category Utility
 */
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
export type Constructor<T> = new (...args: any[]) => T;
 
/**
 * @category Utility
 */
export type InstanceType<C> = C extends Constructor<infer T> ? T : never;
 
/**
 * @category Utility
 */
export type MapBase<K, V> = {
  get: (key: K) => V | undefined;
  set: (key: K, value: V) => void;
  delete: (key: K) => void;
  has: (key: K) => boolean;
};
 
/**
 * @category Utility
 */
export type SetBase<V> = {
  add: (value: V) => SetBase<V>;
  delete: (value: V) => void;
  has: (value: V) => boolean;
};
 
const EMPTY__ignored = {} as const;
 
/**
 * @ignore
 * @internal
 */
export type EmptyLiteral = typeof EMPTY__ignored;
 
/**
 * @ignore
 * @internal
 */
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
export type Spread<A extends readonly [...any]> = A extends [infer L]
  ? L extends null | undefined
    ? EmptyLiteral
    : L
  : A extends [infer L, ...infer R]
    ? SpreadTwo<L extends null | undefined ? EmptyLiteral : L, Spread<R>>
    : unknown;
 
// --------------------
 
type OptionalPropertyNames<T> = {
  /* eslint-disable-next-line @typescript-eslint/no-empty-object-type */
  [K in keyof T]-?: {} extends { [P in K]: T[K] } ? K : never;
}[keyof T];
 
type SpreadProperties<L, R, K extends keyof L & keyof R> = {
  [P in K]: L[P] | Exclude<R[P], undefined>;
};
 
type Id<T> = T extends infer U ? { [K in keyof U]: U[K] } : never;
 
type SpreadTwo<L, R> = Id<
  Pick<L, Exclude<keyof L, keyof R>> &
    Pick<R, Exclude<keyof R, OptionalPropertyNames<R>>> &
    Pick<R, Exclude<OptionalPropertyNames<R>, keyof L>> &
    SpreadProperties<L, R, OptionalPropertyNames<R> & keyof L>
>;