Skip to content

Commit

Permalink
fix onElementConnected
Browse files Browse the repository at this point in the history
  • Loading branch information
mucsi96 committed May 5, 2024
1 parent 3864918 commit d3b26c7
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/Avatar/Avatar.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const meta: Meta = {
</button>
<div popover bt id="avatar-popover">
<ul bt-dropdown-menu>
<li separated>
<li bt-separated>
<p>John Doe</p>
<p>[email protected]</p>
</li>
Expand Down
4 changes: 2 additions & 2 deletions src/DropdownMenu/DropdownMenu.css
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ ul[bt-dropdown-menu] {
--padding-bottom: 0.75rem;
}

&[separated] {
&[bt-separated] {
position: relative;
margin-bottom: var(--separator-gap);

Expand All @@ -43,7 +43,7 @@ ul[bt-dropdown-menu] {
}
}

[separated] + & {
[bt-separated] + & {
margin-top: var(--separator-gap);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/DropdownMenu/DropdownMenu.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ const meta: Meta = {
</button>
<div popover bt id="dropdown-menu-popover">
<ul bt-dropdown-menu>
<li separated>
<li bt-separated>
<p>John Doe</p>
<p>[email protected]</p>
</li>
<li><a href="#">Menu item 1</a></li>
<li><a href="#">Menu item 2</a></li>
<li separated><a href="#">Menu item 3</a></li>
<li bt-separated><a href="#">Menu item 3</a></li>
<li><button type="button">Sign out</button></li>
</ul>
</div>`;
Expand Down
7 changes: 6 additions & 1 deletion src/Table/RowSelector.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { injectStyles } from '../utils';
import { injectStyles, onElementConnected } from '../utils';
import styles from './RowSelector.css?raw&inline';
import svg from './RowSelector.svg?raw&inline';

injectStyles(styles);

onElementConnected('td[bt-row-selector]', (element) => {
element.insertAdjacentHTML('beforeend', svg);
});
9 changes: 1 addition & 8 deletions src/Table/Table.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import { injectStyles, onElementConnected } from '../utils';
import { injectStyles } from '../utils';
import styles from './Table.css?raw&inline';
import svg from './RowSelector.svg?raw&inline';

injectStyles(styles);

onElementConnected('table[bt]', (element) => {
element.querySelectorAll('td[bt-row-selector]').forEach((node) => {
node.insertAdjacentHTML('beforeend', svg);
});
});
52 changes: 39 additions & 13 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,47 @@ export function injectStyles(styles: string) {
const listeners: Record<string, (element: HTMLElement) => void> = {};

const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === 'childList') {
mutation.addedNodes.forEach((node) => {
if (node instanceof HTMLElement) {
Object.entries(listeners).forEach(([selector, callback]) => {
if (node.matches(selector)) {
callback(node);
}
});
}
});
const connected = new Set<HTMLElement>();
const selector = Object.keys(listeners).join(',');

for (const { addedNodes, removedNodes } of mutations) {
for (const node of addedNodes) {
if (!(node instanceof HTMLElement)) continue;

if (node.matches(selector)) {
connected.add(node);
}

for (const child of node.querySelectorAll(selector)) {
if (!(child instanceof HTMLElement)) continue;

connected.add(child);
}
}
});
for (const node of removedNodes) {
if (!(node instanceof HTMLElement)) continue;

if (node.matches(selector)) {
connected.delete(node);
}

for (const child of node.querySelectorAll(selector)) {
if (!(child instanceof HTMLElement)) continue;

connected.delete(child);
}
}
}

for (const node of connected) {
Object.entries(listeners).forEach(([selector, callback]) => {
if (node.matches(selector)) {
callback(node);
}
});
}
});
observer.observe(document.body, {
observer.observe(document, {
childList: true,
subtree: true,
});
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"lib": ["esnext", "dom"]
"lib": ["ESNext", "DOM", "DOM.Iterable"]
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules"]
Expand Down

0 comments on commit d3b26c7

Please sign in to comment.