Skip to content

Commit

Permalink
Proof of concept non-component-based css prop.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenh committed Nov 9, 2022
1 parent da9ab14 commit 0e29072
Show file tree
Hide file tree
Showing 13 changed files with 697 additions and 13 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"packages/testing-tachyons",
"packages/testing-tachyons-emotion",
"packages/testing-tachyons-fela",
"packages/testing-tachyons-mui"
"packages/testing-tachyons-mui",
"packages/app-tachyons-fela-prop"
],
"devDependencies": {
"@semantic-release/changelog": "^6.0.1",
Expand Down
24 changes: 24 additions & 0 deletions packages/app-tachyons-fela-prop/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
12 changes: 12 additions & 0 deletions packages/app-tachyons-fela-prop/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
25 changes: 25 additions & 0 deletions packages/app-tachyons-fela-prop/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "app-tachyons-fela-prop",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview"
},
"dependencies": {
"@homebound/truss-testing-tachyons": "workspace:*",
"fela": "^12.2.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-fela": "^12.2.0"
},
"devDependencies": {
"@types/react": "^18.0.24",
"@types/react-dom": "^18.0.8",
"@vitejs/plugin-react": "^2.2.0",
"typescript": "^4.6.4",
"vite": "^3.2.3"
}
}
16 changes: 16 additions & 0 deletions packages/app-tachyons-fela-prop/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useState } from "react";
import { Css } from "@homebound/truss-testing-tachyons";

function App() {
const [count, setCount] = useState(0);
return (
<div>
<h1 css={Css.lightGray.$}>Vite + React</h1>
<div>
<button onClick={() => setCount((count) => count + 1)}>count is {count}</button>
</div>
</div>
);
}

export default App;
46 changes: 46 additions & 0 deletions packages/app-tachyons-fela-prop/src/jsx-dev-runtime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
import { createRenderer } from "fela";
import { render } from "fela-dom";

// Create a renderer
const renderer = createRenderer();
// And then auto-inject any CSS it creates into the DOM
render(renderer);

/**
* Wraps React's JSX runtime (i.e. `createElement`) with `css` prop support.
*
* Unlike the Emotion or native Fela `css` props, we don't create a wrapping
* `EmotionCssPropInternal` or `FelaComponent` component, which adds another
* React component to the component tree.
*
* Instead, we just merge the `css` prop into the `className` prop, and hand
* it right back to React as a `createElement(div, ...)` etc.
*
* Emotion and Fela likely don't do this b/c they access their `renderer`
* instance via a React context, which we cannot access directly b/c it
* would surely cause "different number of hooks" errors due to conditional
* JSX rendering. Which is dumb b/c accessing a context has no side effect,
* so IMO contexts should be accessible w/o the hook API/restrictions.
*
* But, anyway, we avoid that by just accessing our `renderer` as a global
* variable.
*/
export function jsxDEV(type, props = {}, ...children) {
if (props) {
const { css, className, ...otherProps } = props;
if (css) {
// Convert `{ color: "blue" }` --> `a`
const cn = renderer.renderRule(() => css, {});
return _jsxDEV(
type,
{
...otherProps,
className: className ? cn + " " + className : cn,
},
...children,
);
}
}
return _jsxDEV(type, props, ...children);
}
9 changes: 9 additions & 0 deletions packages/app-tachyons-fela-prop/src/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App'

ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<React.StrictMode>
<App />
</React.StrictMode>
)
1 change: 1 addition & 0 deletions packages/app-tachyons-fela-prop/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
21 changes: 21 additions & 0 deletions packages/app-tachyons-fela-prop/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"allowJs": false,
"skipLibCheck": true,
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"moduleResolution": "Node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
}
9 changes: 9 additions & 0 deletions packages/app-tachyons-fela-prop/tsconfig.node.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"composite": true,
"module": "ESNext",
"moduleResolution": "Node",
"allowSyntheticDefaultImports": true
},
"include": ["vite.config.ts"]
}
7 changes: 7 additions & 0 deletions packages/app-tachyons-fela-prop/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [react({ jsxImportSource: '.' })]
})
3 changes: 1 addition & 2 deletions packages/testing-tachyons/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"extends": "@homebound/tsconfig/tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"module": "ES2022",
"target": "es5",
"sourceMap": true,
"downlevelIteration": true,
"outDir": "build/",
"jsx": "react"
},
Expand Down
Loading

0 comments on commit 0e29072

Please sign in to comment.