Skip to content

Commit

Permalink
ref!: remove insertAfter
Browse files Browse the repository at this point in the history
  • Loading branch information
Sv443 committed May 16, 2024
1 parent be194cb commit fadebf0
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 39 deletions.
6 changes: 6 additions & 0 deletions .changeset/witty-clocks-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@sv443-network/userutils": major
---

Removed the function `insertAfter()` because the DOM API already has the method [`insertAdjacentElement()`](https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentElement) that has the same functionality and even four positions to pick from.
To get the same behavior as `insertAfter(refElem, newElem)`, you can use `refElem.insertAdjacentElement("afterend", newElem)`
1 change: 0 additions & 1 deletion README-summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ or view the documentation of previous major releases:
- DOM:
- [SelectorObserver](https://github.com/Sv443-Network/UserUtils#selectorobserver) - class that manages listeners that are called when selectors are found in the DOM
- [getUnsafeWindow()](https://github.com/Sv443-Network/UserUtils#getunsafewindow) - get the unsafeWindow object or fall back to the regular window object
- [insertAfter()](https://github.com/Sv443-Network/UserUtils#insertafter) - insert an element as a sibling after another element
- [addParent()](https://github.com/Sv443-Network/UserUtils#addparent) - add a parent element around another element
- [addGlobalStyle()](https://github.com/Sv443-Network/UserUtils#addglobalstyle) - add a global style to the page
- [preloadImages()](https://github.com/Sv443-Network/UserUtils#preloadimages) - preload images into the browser cache for faster loading later on
Expand Down
29 changes: 0 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ View the documentation of previous major releases:
- [**DOM:**](#dom)
- [SelectorObserver](#selectorobserver) - class that manages listeners that are called when selectors are found in the DOM
- [getUnsafeWindow()](#getunsafewindow) - get the unsafeWindow object or fall back to the regular window object
- [insertAfter()](#insertafter) - insert an element as a sibling after another element
- [addParent()](#addparent) - add a parent element around another element
- [addGlobalStyle()](#addglobalstyle) - add a global style to the page
- [preloadImages()](#preloadimages) - preload images into the browser cache for faster loading later on
Expand Down Expand Up @@ -463,34 +462,6 @@ document.body.dispatchEvent(mouseEvent);

<br>

### insertAfter()
Usage:
```ts
insertAfter(beforeElement: Element, afterElement: Element): Element
```

Inserts the element passed as `afterElement` as a sibling after the passed `beforeElement`.
The passed `afterElement` will be returned.

⚠️ This function needs to be run after the DOM has loaded (when using `@run-at document-end` or after `DOMContentLoaded` has fired).

<details><summary><b>Example - click to view</b></summary>

```ts
import { insertAfter } from "@sv443-network/userutils";

// insert a <div> as a sibling next to an element
const beforeElement = document.querySelector("#before");
const afterElement = document.createElement("div");
afterElement.innerText = "After";

insertAfter(beforeElement, afterElement);
```

</details>

<br>

### addParent()
Usage:
```ts
Expand Down
9 changes: 0 additions & 9 deletions lib/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,6 @@ export function getUnsafeWindow() {
}
}

/**
* Inserts {@linkcode afterElement} as a sibling just after the provided {@linkcode beforeElement}
* @returns Returns the {@linkcode afterElement}
*/
export function insertAfter(beforeElement: Element, afterElement: Element) {
beforeElement.parentNode?.insertBefore(afterElement, beforeElement.nextSibling);
return afterElement;
}

/**
* Adds a parent container around the provided element
* @returns Returns the new parent element
Expand Down

0 comments on commit fadebf0

Please sign in to comment.