Skip to content

Commit

Permalink
Merge pull request #1 from rkrupinski/example
Browse files Browse the repository at this point in the history
example
  • Loading branch information
rkrupinski authored Jul 29, 2023
2 parents a4530cf + 7309094 commit ebcd0f3
Show file tree
Hide file tree
Showing 24 changed files with 1,740 additions and 63 deletions.
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,64 @@
# @rkrupinski/react

An experimental, lightweight [React](https://react.dev/) alternative.

- [Getting started](#getting-started)

## Getting started

Install:

```
yarn add @rkrupinski/react
```

Make sure to set:

```json
{
"jsx": "react"
}
```

in your `tsconfig.json` -> `"compilerOptions"`.

Now you're all set:

```tsx
/* @jsx createElement */
/* @jsxFrag Fragment */

import {
render,
useState,
useEffect,
createElement,
Fragment,
type FC,
} from "@rkrupinski/react";

const App: FC = () => {
const [clicked, setClicked] = useState(0);

useEffect(() => {
console.log(`Clicked ${clicked} times`);
}, [clicked]);

return (
<>
<h1>Hello!</h1>
<button
onClick={() => {
setClicked((c) => c + 1);
}}
>
{clicked}
</button>
</>
);
};

const root = document.querySelector("#root") as HTMLElement;

render(<App />, root);
```
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
"packages/*"
],
"scripts": {
"clean": "wsrun -mc clean",
"typecheck": "wsrun -mc typecheck",
"lint": "wsrun -mc lint",
"test": "wsrun -mc test",
"build": "wsrun -tc build"
"clean": "wsrun -mc clean",
"typecheck": "wsrun -mc typecheck",
"lint": "wsrun -mc lint",
"test": "wsrun -mc test",
"start": "wsrun -mc start",
"build": "wsrun -tc build"
},
"devDependencies": {
"wsrun": "5.2.4"
Expand Down
1 change: 1 addition & 0 deletions packages/example/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"@rkrupinski/prettierconfig"
3 changes: 3 additions & 0 deletions packages/example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @rkrupinski/react

- [Docs](https://github.com/rkrupinski/react#readme)
18 changes: 18 additions & 0 deletions packages/example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "example",
"private": true,
"version": "0.0.0",
"scripts": {
"clean": "rm -rf dist",
"start": "parcel src/index.html --open",
"build": "parcel build src/index.html"
},
"dependencies": {
"@rkrupinski/react": "*",
"todomvc-app-css": "2.4.2"
},
"devDependencies": {
"@rkrupinski/prettierconfig": "1.0.1",
"parcel": "2.9.3"
}
}
43 changes: 43 additions & 0 deletions packages/example/src/components/TodoInput/TodoInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* @jsx createElement */

import {
createElement,
useState,
useCallback,
type FC,
} from '@rkrupinski/react';

export type TodoInputProps = {
onNewTodo: (text: string) => void;
};

export const TodoInput: FC<TodoInputProps> = ({ onNewTodo }) => {
const [text, setText] = useState('');

const handleInput = useCallback(
(e: any) => {
setText(e.target.value);
},
[setText],
);

const handleKeyDown = useCallback(
(e: any) => {
if (e.keyCode !== 13) return;

onNewTodo(e.target.value);
setText('');
},
[onNewTodo, setText],
);

return (
<input
value={text}
onInput={handleInput}
onKeyDown={handleKeyDown}
className="new-todo"
placeholder="What needs to be done?"
/>
);
};
1 change: 1 addition & 0 deletions packages/example/src/components/TodoInput/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './TodoInput';
26 changes: 26 additions & 0 deletions packages/example/src/components/TodoItem/TodoItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* @jsx createElement */

import { createElement, type FC } from '@rkrupinski/react';

import type { Todo } from '../../types';

export type TodoItemProps = {
data: Todo;
onToggle: (todo: Todo) => void;
onDestroy: (todo: Todo) => void;
};

export const TodoItem: FC<TodoItemProps> = ({ data, onToggle, onDestroy }) => (
<li className={data.completed ? 'completed' : ''}>
<div className="view">
<input
className="toggle"
type="checkbox"
checked={data.completed}
onChange={() => onToggle(data)}
/>
<label>{data.body}</label>
<button className="destroy" onClick={() => onDestroy(data)} />
</div>
</li>
);
1 change: 1 addition & 0 deletions packages/example/src/components/TodoItem/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './TodoItem';
25 changes: 25 additions & 0 deletions packages/example/src/components/TodoList/TodoList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* @jsx createElement */

import { createElement, type FC } from '@rkrupinski/react';

import type { Todo } from '../../types';
import { TodoItem } from '../TodoItem';

export type TodoListProps = {
todos: Todo[];
onToggle: (todo: Todo) => void;
onDestroy: (todo: Todo) => void;
};

export const TodoList: FC<TodoListProps> = ({ todos, onToggle, onDestroy }) => (
<ul className="todo-list">
{todos.map(todo => (
<TodoItem
key={todo.id}
data={todo}
onToggle={onToggle}
onDestroy={onDestroy}
/>
))}
</ul>
);
1 change: 1 addition & 0 deletions packages/example/src/components/TodoList/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './TodoList';
26 changes: 26 additions & 0 deletions packages/example/src/components/TodoNav/TodoNav.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* @jsx createElement */

import { createElement, type FC } from '@rkrupinski/react';

import { view, type View } from '../../types';

export type TodoNavProps = {
current: View;
onChange: (newView: View) => void;
};

export const TodoNav: FC<TodoNavProps> = ({ current, onChange }) => (
<ul className="filters">
{view.map(v => (
<li key={v}>
<a
className={v === current ? 'selected' : ''}
href={`#${v}`}
onClick={() => onChange(v)}
>
{v}
</a>
</li>
))}
</ul>
);
1 change: 1 addition & 0 deletions packages/example/src/components/TodoNav/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './TodoNav';
36 changes: 36 additions & 0 deletions packages/example/src/components/ToggleAll/ToggleAll.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* @jsx createElement */
/* @jsxFrag Fragment */

import {
createElement,
Fragment,
useCallback,
type FC,
} from '@rkrupinski/react';

export type ToggleAllProps = {
all: boolean;
onToggle: (all: boolean) => void;
};

export const ToggleAll: FC<ToggleAllProps> = ({ all, onToggle }) => {
const handleToggle = useCallback(
(e: any) => {
onToggle(e.target.checked);
},
[onToggle],
);

return (
<>
<input
id="toggle-all"
className="toggle-all"
type="checkbox"
checked={all}
onChange={handleToggle}
/>
<label for="toggle-all" />
</>
);
};
1 change: 1 addition & 0 deletions packages/example/src/components/ToggleAll/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './ToggleAll';
12 changes: 12 additions & 0 deletions packages/example/src/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>@rkrupinski/react</title>
<script type="module" src="index.tsx"></script>
</head>
<body>
<div id="root"></div>
</body>
</html>
Loading

0 comments on commit ebcd0f3

Please sign in to comment.