Skip to content

Commit

Permalink
publish pure-react
Browse files Browse the repository at this point in the history
  • Loading branch information
Pistonight committed Oct 27, 2024
1 parent f44dead commit 10b617f
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 19 deletions.
38 changes: 19 additions & 19 deletions .github/workflows/jsr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ jobs:
- run: task check
- run: task test

# build-react:
# name: Build (React)
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v4
# - uses: denoland/setup-deno@v1
# with:
# deno-version: v1.x
# - uses: arduino/setup-task@v2
# with:
# version: 3.x
# repo-token: ${{ secrets.GITHUB_TOKEN }}
# - uses: actions/setup-node@v4
# with:
# node-version: 20.x
# - run: npm install
# working-directory: ./react
# - run: task react:check
# - run: task react:test
build-react:
name: Build (React)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- uses: arduino/setup-task@v2
with:
version: 3.x
repo-token: ${{ secrets.GITHUB_TOKEN }}
- uses: actions/setup-node@v4
with:
node-version: 20.x
- run: npm install
working-directory: ./react
- run: task react:check
- run: task react:test
1 change: 1 addition & 0 deletions react/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@jsr:registry=https://npm.jsr.io
21 changes: 21 additions & 0 deletions react/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Michael

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
10 changes: 10 additions & 0 deletions react/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# pure-react
![JSR Version](https://img.shields.io/jsr/v/@pistonite/pure-react)

React utilities (hooks) for [pure](https://jsr.io/@pistonite/pure)

## Setup
Install the package from JSR with your package manager
```bash
npx jsr install @pistonite/pure @pisontite/pure-react
```
19 changes: 19 additions & 0 deletions react/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: '3'

tasks:
check:
desc: Check the code
cmds:
- npx tsc
- deno lint
- npx jsr publish --dry-run

test:
desc: Run the tests
cmds:
- echo "No tests"

publish:
desc: Publish the package
cmds:
- npx jsr publish
16 changes: 16 additions & 0 deletions react/deno.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@pistonite/pure-react",
"version": "0.0.1",
"exports": {
".": "./src/index.ts",
},
"publish": {
"exclude": ["package-lock.json", "node_modules/**", "Taskfile.yml"],
},
"lint": {
"rules": {
"include": ["no-slow-types", "missing-explicit-return-type"],
"exclude": ["no-explicit-any"],
},
},
}
10 changes: 10 additions & 0 deletions react/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"peerDependencies": {
"react": "^18",
"@pistonite/pure": "npm:@jsr/pistonite__pure@^0.0.10"
},
"devDependencies": {
"@types/react": "^18.3.12",
"typescript": "^5.5.4"
}
}
1 change: 1 addition & 0 deletions react/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./pref.ts";
87 changes: 87 additions & 0 deletions react/src/pref.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* # pure-react/pref
*
* React bindings for `pure/pref`
*
* ## Dark Mode
* The `useDark` hook returns the current dark mode state, and subscribes to changes.
* To set the dark mode, use `setDark` from `pure/pref`.
* ```tsx
* import { useDark } from "@pistonite/pure-react";
*
* const MyComponent = () => {
* // will re-render when dark mode changes
* const dark = useDark();
*
* return <div>{dark ? "Mode: Dark" : "Mode: Light"}</div>;
* };
* ```
*
* ## Locale
* The `useLocale` hook returns the current locale, and subscribes to changes.
*
* ```tsx
* import { useLocale } from "@pistonite/pure-react";
*
* const MyComponent = () => {
* const locale = useLocale();
*
* return <div>Current Locale is: {locale}</div>;
* };
* ```
*
* @module
*/

import { useState, useEffect } from "react";

import {
addDarkSubscriber,
addLocaleSubscriber,
getLocale,
isDark,
removeDarkSubscriber,
removeLocaleSubscriber,
} from "@pistonite/pure/pref";

/**
* Hook to get the current dark mode state
*/
export const useDark = (): boolean => {
const [value, setValue] = useState(isDark);
useEffect(() => {
const dark = isDark();
if (dark !== value) {
setValue(dark);
}
const subscriber = (dark: boolean) => {
setValue(dark);
};
addDarkSubscriber(subscriber);
return () => {
removeDarkSubscriber(subscriber);
};
}, []);
return value;
};

/**
* Hook to get the current locale
*/
export const useLocale = (): string => {
const [locale, setLocale] = useState(getLocale);
useEffect(() => {
const l = getLocale();
if (l !== locale) {
setLocale(l);
}
const subscriber = (locale: string) => {
setLocale(locale);
};
addLocaleSubscriber(subscriber);
return () => {
removeLocaleSubscriber(subscriber);
};
}, []);
return locale;
};
23 changes: 23 additions & 0 deletions react/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"lib": ["ES2021", "DOM", "DOM.Iterable"],
"module": "ESNext",
"skipLibCheck": true,

/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",

/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src"]
}

0 comments on commit 10b617f

Please sign in to comment.