Skip to content

Commit

Permalink
StyleSheet component (#41)
Browse files Browse the repository at this point in the history
* stylesheet component

* add changeset
  • Loading branch information
nsaunders authored May 31, 2024
1 parent 5e5a44c commit 5e3ad90
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changeset/spotty-dragons-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@embellish/react": minor
---

Replaced the `styleSheet` function with a `StyleSheet` component.
Original file line number Diff line number Diff line change
@@ -1,38 +1,15 @@
import type {
Condition,
Conditions,
Digit,
Letter,
OnlyChars,
UppercaseLetter,
ValidConditionName,
} from "@embellish/core";
import type {
Component,
ComponentProps,
CSSProperties,
ElementType,
PropsWithoutRef,
PropsWithRef,
RefAttributes,
} from "react";

export { createHooks, createConditions } from "@embellish/core";

export type ValidComponentDisplayName<Name> =
Name extends `${UppercaseLetter}${infer Tail}`
? OnlyChars<Letter | Digit, Tail>
: never;
import type React, { CSSProperties } from "react";

export type ValidStylePropName<Name> = Name extends `${Letter}${infer Tail}`
? OnlyChars<Letter | Digit, Tail>
: never;

export type ComponentPropsWithRef<C extends ElementType> = PropsWithRef<
C extends new (props: infer P) => Component<unknown, unknown>
? PropsWithoutRef<P> & RefAttributes<InstanceType<C>>
: ComponentProps<C>
>;
import type {
ComponentPropsWithRef,
ValidComponentDisplayName,
ValidStylePropName,
} from "./types";

export function createComponent<
const DisplayName extends string,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { createLocalConditions } from "@embellish/core";
import { createElement, forwardRef } from "react";

export { createHooks, createConditions } from "@embellish/core";

const unitlessNumbers = new Set([
"animationIterationCount",
"aspectRatio",
Expand Down
17 changes: 17 additions & 0 deletions packages/react/src/hooks.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { HookId, Selector } from "@embellish/core";
import { createHooks as createHooksImpl } from "@embellish/core";

export function createHooks<Hooks extends Selector[]>(
hooks: Hooks,
): {
StyleSheet(): JSX.Element;
hooks: { [Hook in Hooks[number]]: HookId };
} {
const { styleSheet, ...rest } = createHooksImpl(hooks);
return {
...rest,
StyleSheet() {
return <style dangerouslySetInnerHTML={{ __html: styleSheet() }} />;
},
};
}
3 changes: 3 additions & 0 deletions packages/react/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { createHooks } from "./hooks.js";
export { createConditions } from "@embellish/core";
export { createComponent } from "./component.js";
28 changes: 28 additions & 0 deletions packages/react/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type {
Digit,
Letter,
OnlyChars,
UppercaseLetter,
} from "@embellish/core";
import type {
Component,
ComponentProps,
ElementType,
PropsWithRef,
RefAttributes,
} from "react";

export type ValidComponentDisplayName<Name> =
Name extends `${UppercaseLetter}${infer Tail}`
? OnlyChars<Letter | Digit, Tail>
: never;

export type ValidStylePropName<Name> = Name extends `${Letter}${infer Tail}`
? OnlyChars<Letter | Digit, Tail>
: never;

export type ComponentPropsWithRef<C extends ElementType> = PropsWithRef<
C extends new (props: infer P) => Component<unknown, unknown>
? PropsWithRef<P> & RefAttributes<InstanceType<C>>
: ComponentProps<C>
>;
5 changes: 4 additions & 1 deletion packages/react/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"extends": "@tsconfig/strictest",
"compilerOptions": {
"module": "ES2015",
"moduleResolution": "Node",
"noEmit": true,
"checkJs": false
"checkJs": false,
"jsx": "react-jsx"
},
"include": ["src"]
}
4 changes: 2 additions & 2 deletions packages/react/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ export default defineConfig({
build: {
minify: false,
lib: {
entry: resolve(__dirname, "src", "index.js"),
entry: resolve(__dirname, "src", "index.ts"),
formats: ["es"],
},
rollupOptions: {
external: ["react"],
external: ["react", "react/jsx-runtime"],
output: {
preserveModules: true,
preserveModulesRoot: "src",
Expand Down

0 comments on commit 5e3ad90

Please sign in to comment.