Skip to content

Commit

Permalink
Rename createHooks generic params.
Browse files Browse the repository at this point in the history
  • Loading branch information
nsaunders committed Jun 19, 2024
1 parent 8e3d2e5 commit f072c28
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
14 changes: 7 additions & 7 deletions docs/api/react.createhooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ Creates the specified CSS hooks.
**Signature:**

```typescript
export declare function createHooks<Hooks extends Selector[]>(hooks: Hooks): {
export declare function createHooks<Selectors extends Selector[]>(selectors: Selectors): {
StyleSheet(): JSX.Element;
hooks: {
[Hook in Hooks[number]]: HookId;
[Hook in Selectors[number]]: HookId;
};
};
```
Expand All @@ -37,24 +37,24 @@ Description
</th></tr></thead>
<tbody><tr><td>

hooks
selectors


</td><td>

Hooks
Selectors


</td><td>

The hooks to create.
The selector logic for each hook


</td></tr>
</tbody></table>
**Returns:**

{ StyleSheet(): JSX.Element; hooks: { \[Hook in Hooks\[number\]\]: [HookId](./react.hookid.md)<!-- -->; }; }
{ StyleSheet(): JSX.Element; hooks: { \[Hook in Selectors\[number\]\]: [HookId](./react.hookid.md)<!-- -->; }; }

The created hooks along with the `StyleSheet` component required to support them.
A set of hooks implementing the specified selector logic along with the `StyleSheet` component required to support them

2 changes: 1 addition & 1 deletion docs/api/react.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Creates the specified conditions based on available hooks.
</td></tr>
<tr><td>

[createHooks(hooks)](./react.createhooks.md)
[createHooks(selectors)](./react.createhooks.md)


</td><td>
Expand Down
16 changes: 9 additions & 7 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,19 +156,21 @@ export type Selector =
export type HookId = Branded<string, "HookId">;

/** @internal */
export function createHooks<Hooks extends Selector[]>(hooks: Hooks) {
const hookIds = Object.fromEntries(
hooks.map(hook => [hook, createHash(hook)]),
) as { [Hook in Hooks[number]]: HookId };
export function createHooks<Selectors extends Selector[]>(
selectors: Selectors,
) {
const hooks = Object.fromEntries(
selectors.map(selector => [selector, createHash(selector)]),
) as { [Hook in Selectors[number]]: HookId };
return {
styleSheet() {
const indent = Array(2).fill(space).join("");
return `*${space}{${newline}${Object.entries(hookIds)
return `*${space}{${newline}${Object.entries(hooks)
.flatMap(([, id]) => [
`${indent}--${id}-0:${space}initial;`,
`${indent}--${id}-1:${space};`,
])
.join(newline)}${newline}}${newline}${Object.entries(hookIds)
.join(newline)}${newline}}${newline}${Object.entries(hooks)
.flatMap(([def, id]) => {
if (def.startsWith("@")) {
return [
Expand All @@ -189,7 +191,7 @@ export function createHooks<Hooks extends Selector[]>(hooks: Hooks) {
})
.join(newline)}`;
},
hooks: hookIds,
hooks,
};
}

Expand Down
15 changes: 8 additions & 7 deletions packages/react/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ import { createElement } from "react";
/**
* Creates the specified CSS hooks.
*
* @typeParam Hooks - The type of the hooks to create.
* @param hooks - The hooks to create.
* @typeParam Selectors - The type of the selector logic for each hook
* @param selectors - The selector logic for each hook
*
* @returns The created hooks along with the `StyleSheet` component required to support them.
* @returns A set of hooks implementing the specified selector logic along with
* the `StyleSheet` component required to support them
*
* @public
*/
export function createHooks<Hooks extends Selector[]>(
hooks: Hooks,
export function createHooks<Selectors extends Selector[]>(
selectors: Selectors,
): {
StyleSheet(): JSX.Element;
hooks: { [Hook in Hooks[number]]: HookId };
hooks: { [Hook in Selectors[number]]: HookId };
} {
const { styleSheet, ...rest } = createHooksImpl(hooks);
const { styleSheet, ...rest } = createHooksImpl(selectors);
return {
...rest,
StyleSheet() {
Expand Down

0 comments on commit f072c28

Please sign in to comment.