Skip to content

Class: DOM ​

DOM is a simple utility class for dom manipulation. An instance is available on BdApi.

Extends ​

Accessors ​

screenHeight ​

Get Signature ​

get screenHeight(): number

The current height of the user's screen.

Returns ​

number

Inherited from ​

BaseDOM.screenHeight


screenWidth ​

Get Signature ​

get screenWidth(): number

The current width of the user's screen.

Returns ​

number

Inherited from ​

BaseDOM.screenWidth

Methods ​

addStyle() ​

addStyle(id, css): void

Adds a <style> to the document with the given ID.

Parameters ​

ParameterTypeDescription
idstringID to use for style element
cssstringCSS to apply to the document

Returns ​

void


animate() ​

animate(update, duration, options?): () => void

Utility to help smoothly animate using JavaScript.

Parameters ​

ParameterTypeDescription
update(p) => voidRender function indicating the style should be updated
durationnumberDuration in ms to animate for
options?AnimateOptionsOptions to customize the animation

Returns ​

() => void

Inherited from ​

BaseDOM.animate


createElement() ​

createElement<T>(tag, options?, ...children?): HTMLElement

Utility function to make creating DOM elements easier. Acts similarly to React.createElement

Type Parameters ​

Type Parameter
T extends keyof HTMLElementTagNameMap

Parameters ​

ParameterTypeDescription
tagTHTML tag name to create
options?Partial<HTMLElementTagNameMap[T]>Options object to customize the element
...children?(string | Node)[]Child nodes to add

Returns ​

HTMLElement

The created HTML element

Inherited from ​

BaseDOM.createElement


onAdded() ​

onAdded(selector, callback): void | (() => void)

Adds a listener for when a node matching a selector is added to the document body. The listener is automatically removed upon firing. The callback is given the matching element.

Parameters ​

ParameterTypeDescription
selectorstringA CSS selector for the node to wait for
callback() => voidFunction to be performed on event

Returns ​

void | (() => void)

Inherited from ​

BaseDOM.onAdded


onRemoved() ​

onRemoved(node, callback): () => void

Adds a listener for when the node is removed from the document body.

Parameters ​

ParameterTypeDescription
nodeHTMLElementNode to be observed
callback() => voidFunction to run when removed

Returns ​

() => void

Inherited from ​

BaseDOM.onRemoved


parseHTML() ​

parseHTML(html, fragment?): Node | NodeListOf<ChildNode>

Parses a string of HTML and returns the results. If the second parameter is true, the parsed HTML will be returned as a document fragment. This is extremely useful if you have a list of elements at the top level, they can then be appended all at once to another node.

If the second parameter is false, then the return value will be the list of parsed nodes and there were multiple top level nodes, otherwise the single node is returned.

Parameters ​

ParameterTypeDefault valueDescription
htmlstringundefinedHTML to be parsed
fragment?booleanfalseWhether or not the return should be the raw DocumentFragment

Returns ​

Node | NodeListOf<ChildNode>

The result of HTML parsing

Inherited from ​

BaseDOM.parseHTML


removeStyle() ​

removeStyle(id): void

Removes a <style> from the document corresponding to the given ID.

Parameters ​

ParameterTypeDescription
idstringID used for the style element

Returns ​

void