dom-ts
TypeScript icon, indicating that this package has built-in type declarations

11.0.0 • Public • Published

dom-ts

PLEASE NOTE this is not ready for production use. There's still a lot to do for DX to make it friendly.

semantic-release

fp-ts compatible implementations of DOM interfaces and related API's, as documented under DOM interfaces at Mozilla.

Installation

There are two release branches: @latest and @next. If you'd like to try the latest features, replace dom-ts with dom-ts@next.

Yarn

yarn add dom-ts fp-ts fp-ts-contrib

NPM

npm install dom-ts fp-ts fp-ts-contrib

Library Structure

Each DOM interface (Node, ChildNode,Event)

Configuration

Legend

Icon Status Description
✔️ Implemented, available for use.
Unimplemented, with plans to implement.
Unimplemented, with no plans to implement.

Compatibility Table

We only add functions for methods on interfaces. We don't need to add functions that get values.

Deprecated interfaces and methods are displayed in this list, but are noted as deprecated. They will not be implemented. Obsolete interfaces and methods are not displayed in this list.

Status Methods Base Interface Notes
Attr Interface has no specific methods
CDATASection Interface has no specific methods
CharacterData
appendData
deleteData
insertData
replaceData
substringData
remove ChildNode
before
after
replaceWith
Constructor CustomEvent
initCustomEvent
Constructor Document
adoptNode
createAttribute Constructors are available in their respective modules
createAttributeNS ".."
createCDATASection ".."
createComment ".."
createEvent ".."
createNodeIterator ".."
createProcessingInstruction ".."
createRange ".."
createTextNode ".."
createTreeWalker ".."
exitPictureInPicture ".."
exitPointerLock ".."
getElementsByClassName
getElementsByTagName
getElementsByTagNameNS
hasStorageAccess
importNode
releaseCapture
requestStorageAccess
getElementById
querySelector
querySelectorAll
DocumentFragment Interface has no specific methods
DocumentType Interface has no specific methods
DOMError Deprecated
DOMException Interface has no specific methods, but will provide types for all different types of errors
DOMImplementation Deprecated, but will need to double check.
DOMString Interface has no specific methods
DOMTimeStamp Interface has no specific methods
DOMStringList Deprecated
DOMTokenList
item
contains
add
remove
replace
supports
toggle
forEach (map)
entries (toIterator)
keys
values
Constructor Element
attachShadow
animate
closests
createShadowRoot Deprecated
computedStyleMap
getAnimations
getAttribute
getAttributenames
getAttributeNS
getboundingClientRect
getClientRects
getElementsByClassName
getElementsByTagName
getElementsbyTagnameNS
hasAttribute
hasAttributes
hasPointerCapture
insertAdjacentElement
insertAdjacentElement
insertAdjacentText
matches
pseudo
querySelector
querySelectorAll
releasePointCapture
removeAttribute
removeAttributeNS
requestFullScreen
requestPointerLock
scroll
scrollby
scrollintoView
scrollTo
setAttribute
setAttributeNS
setCapture
setPointerCapture
toggleAttribute
Constructor Event
composedPath
preventDefault
stopImmediatePropogation
stopPropogation
EventTarget
HTMLCollection
MutationObserver
MutationRecord
NamedNodeMap
NamedNodeMap
Node
NodeFilter
NodeIterator
NodeList
ParentNode
ProcessingInstruction
Selection
Range
Text
TextDecoder
TextEncoder
TimeRanges
TreeWalker
URL
Window
Worker
XMLDocument

Usage

Examples

Add Custom Elements, Events

The modules under dom-ts/meta exports maps that define relationships between the following:

  • tagName
  • Element
  • EventMap

We use this to infer some types from each other, like EventMap from Element

This is currently unsupported due to the way that Typescript has structured it's type definitions. An issue has been opened up outlining these concerns in Microsoft/Typescript #40689

Custom Elements and Custom Events

We need to use module augmentation via declaration merging in order to have the new types available in the API.

Defining Elements

We have to define an element, by providing a tagName, Element and EventMap

Define an Element. We'll define a HTMLGroovyElement, named after the Emperors' New Groove

import { either as E } from "fp-ts"
import { pipe } from "fp-ts/lib/function"
import { document, eventTarget, readerIOEither as RIOE } from "../src"
import { MetaHTMLElement } from "../src/meta"

/**
 * @summary
 * This element is always groovy,
 * emitting the `groove` event every 4 seconds.
 */
export interface HTMLGroovyElement extends HTMLElement {}

export interface GrooveEvent {
  grooviness: number
  energy: number
  flow: number
  espanol: boolean
}

export interface HTMLGroovyElementEventMap extends HTMLElementEventMap {
  groove: GrooveEvent
}

export type MetaHTMLGroovyElement = MetaHTMLElement<
  "groovy",
  HTMLGroovyElement,
  HTMLGroovyElementEventMap
>

declare module "../src/meta" {
  export interface Custom {
    // Only HTMLElement's
    HTMLElements: MetaHTMLGroovyElement

    // Only SVGElement's
    // SVGElements: ...

    // Non {HTML,SVG}Element's
    // Elements
  }
}

// Now we get intellisense

const program = pipe(
  document.createElement("groovy"),
  RIOE.chainReaderIOK(
    eventTarget.addEventListener(
      "groove",
      (event) => () => console.dir(event),
      { capture: true }
    )
  )
)

program(globalThis.document)

Upcoming

Generated Meta maps using the typescript compiler, so any updates are generated using the installed version of typescript.

Readme

Keywords

none

Package Sidebar

Install

npm i dom-ts

Weekly Downloads

36

Version

11.0.0

License

MIT

Unpacked Size

155 kB

Total Files

76

Last publish

Collaborators

  • waynevanson