Skip to content

Commit

Permalink
retain masks across useId invocations
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Dec 31, 2023
1 parent 899e9d9 commit fe23841
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
9 changes: 9 additions & 0 deletions hooks/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ let oldBeforeRender = options._render;
let oldAfterDiff = options.diffed;
let oldCommit = options._commit;
let oldBeforeUnmount = options.unmount;
let oldRoot = options._root;

const RAF_TIMEOUT = 100;
let prevRaf;
Expand All @@ -35,6 +36,14 @@ options._diff = vnode => {
if (oldBeforeDiff) oldBeforeDiff(vnode);
};

options._root = (vnode, parentDom) => {
if (parentDom._children && parentDom._children._mask) {
vnode._mask = parentDom._children._mask;
}

if (oldRoot) oldRoot(vnode, parentDom);
};

/** @type {(vnode: import('./internal').VNode) => void} */
options._render = vnode => {
if (oldBeforeRender) oldBeforeRender(vnode);
Expand Down
23 changes: 23 additions & 0 deletions hooks/test/browser/useId.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,4 +433,27 @@ describe('useId', () => {
rerender();
expect(first).not.to.equal(scratch.innerHTML);
});

it('should return a unique id across invocations of render', () => {
const Id = () => {
const id = useId();
return <div>My id is {id}</div>;
};

const App = props => {
return (
<div>
<Id />
{props.secondId ? <Id /> : null}
</div>
);
};

render(createElement(App, { secondId: false }), scratch);
expect(scratch.innerHTML).to.equal('<div><div>My id is P0-0</div></div>');
render(createElement(App, { secondId: true }), scratch);
expect(scratch.innerHTML).to.equal(
'<div><div>My id is P0-0</div><div>My id is P0-1</div></div>'
);
});
});

0 comments on commit fe23841

Please sign in to comment.