Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useId hook #3402

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions hooks/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,5 @@ export function useDebugValue<T>(value: T, formatter?: (value: T) => any): void;
export function useErrorBoundary(
callback?: (error: any) => Promise<void> | void
): [any, () => void];

export function useId(): string;
21 changes: 21 additions & 0 deletions hooks/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ let previousInternal;
/** @type {number} */
let currentHook = 0;

/** @type {number} */
let currentIdCounter = 0;

/** @type {Array<import('./internal').Component>} */
let afterPaintEffects = [];

Expand Down Expand Up @@ -353,6 +356,24 @@ export function useErrorBoundary(cb) {
];
}

export function useId() {
const state = getHookState(currentIndex++, 11);
if (!state._id) {
currentIdCounter++;

console.log(
currentInternal,
currentInternal._parent._children.indexOf(currentInternal)
);
state._id =
'_P' +
(currentInternal._parent._children.indexOf(currentInternal) +
currentInternal._depth +
Copy link
Member Author

@JoviDeCroock JoviDeCroock Mar 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_domDepth might be more reliable here as sometimes people wrap their ssr with a wrapper for the routing, ... we could also just remove it as the global id counter should take care of it either way

We could even just do

			'_P' +
			currentInternal._rootId + currentIdCounter

The only thing that currently bothers me is the fact that this could be annoying when we talk about plugins, maybe we allow for custom prefixes to help them out?

Something like

export function useId(customPrefix) {
	const state = getHookState(currentIndex++, 11);
	if (!state._id) {
		currentIdCounter++;

		state._id =
			customPrefix || '_P' +
			currentInternal._rootId +
			currentIdCounter
	}
	return state._id;
}

currentIdCounter);
}
return state._id;
}

/**
* After paint effects consumer.
*/
Expand Down
91 changes: 91 additions & 0 deletions hooks/test/browser/useId.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { createElement, render } from 'preact';
import { setupScratch, teardown } from '../../../test/_util/helpers';
import { useId } from 'preact/hooks';

/** @jsx createElement */

describe('useId', () => {
/** @type {HTMLDivElement} */
let scratch;

beforeEach(() => {
scratch = setupScratch();
});

afterEach(() => {
teardown(scratch);
});

it('keeps the id consistent after an update', () => {
function Comp() {
const id = useId();
return <div id={id} />;
}

render(<Comp />, scratch);
const id = scratch.firstChild.getAttribute('id');
expect(scratch.firstChild.getAttribute('id')).to.equal(id);

render(<Comp />, scratch);
expect(scratch.firstChild.getAttribute('id')).to.equal(id);
});

it('ids are unique according to dom-depth', () => {
function Child() {
const id = useId();
const spanId = useId();
return (
<div id={id}>
<span id={spanId}>h</span>
</div>
);
}

function Comp() {
const id = useId();
return (
<div id={id}>
<Child />
</div>
);
}

render(<Comp />, scratch);
expect(scratch.innerHTML).to.equal(
'<div id="_P3"><div id="_P6"><span id="_P7">h</span></div></div>'
);

render(<Comp />, scratch);
expect(scratch.innerHTML).to.equal(
'<div id="_P3"><div id="_P6"><span id="_P7">h</span></div></div>'
);
});

it('ids are unique across siblings', () => {
function Child() {
const id = useId();
return <span id={id}>h</span>;
}

function Comp() {
const id = useId();
return (
<div id={id}>
<Child />
<Child />
<Child />
</div>
);
}

render(<Comp />, scratch);
expect(scratch.innerHTML).to.equal(
'<div id="_P6"><span id="_P9">h</span><span id="_P11">h</span><span id="_P13">h</span></div>'
);

render(<Comp />, scratch);
expect(scratch.innerHTML).to.equal(
'<div id="_P6"><span id="_P9">h</span><span id="_P11">h</span><span id="_P13">h</span></div>'
);
});
});
1 change: 1 addition & 0 deletions mangle.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"$_originalParentDom": "__O",
"$_prevState": "__u",
"$_root": "__",
"$_rootId": "__ri",
"$_diff": "__b",
"$_commit": "__c",
"$_addHookName": "__a",
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/jsx.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./dist/jsx'); // eslint-disable-line
module.exports = require('./dist/server'); // eslint-disable-line
22 changes: 6 additions & 16 deletions server/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
import { VNode } from 'preact';

export interface Options {
interface Options {
shallow?: boolean;
xml?: boolean;
pretty?: boolean | string;
}

type RenderFn = (vnode: VNode, context?: any, options?: Options) => string;
export const render: RenderFn;
export const renderToString: RenderFn;
export const renderToStaticMarkup: RenderFn;

export function shallowRender(vnode: VNode, context?: any): string;

export interface JsxOptions extends Options {
functions?: boolean;
functionNames?: boolean;
skipFalseAttributes?: boolean;
}

export function renderToJsxString(
export function render(vnode: VNode, context?: any, options?: Options): string;
export function renderToString(
vnode: VNode,
context?: any,
options: JsxOptions
options?: Options
): string;
export function shallowRender(vnode: VNode, context?: any): string;
export default render;
Loading