-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5f64cbc
Showing
15 changed files
with
2,085 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Changesets | ||
|
||
Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works | ||
with multi-package repos, or single-package repos to help you version and publish your code. You can | ||
find the full documentation for it [in our repository](https://github.com/changesets/changesets) | ||
|
||
We have a quick list of common questions to get you started engaging with this project in | ||
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"$schema": "https://unpkg.com/@changesets/config@2.3.0/schema.json", | ||
"changelog": "@changesets/cli/changelog", | ||
"commit": false, | ||
"fixed": [], | ||
"linked": [], | ||
"access": "public", | ||
"baseBranch": "main", | ||
"updateInternalDependencies": "patch", | ||
"ignore": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"my-package": patch | ||
--- | ||
|
||
Added the add function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
indent_size = 2 | ||
indent_style = space | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
name: CI | ||
on: | ||
push: | ||
branches: | ||
- "**" | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: pnpm/action-setup@v2 | ||
with: | ||
version: 7 | ||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: 16.x | ||
cache: "pnpm" | ||
|
||
- run: pnpm install --frozen-lockfile | ||
- run: pnpm run lint && pnpm run build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
name: Publish | ||
on: | ||
workflow_run: | ||
workflows: [CI] | ||
branches: [main] | ||
types: [completed] | ||
|
||
concurrency: ${{ github.workflow }}-${{ github.ref }} | ||
|
||
permissions: | ||
contents: write | ||
pull-requests: write | ||
|
||
jobs: | ||
publish: | ||
if: ${{ github.event.workflow_run.conclusion == 'success' }} | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: pnpm/action-setup@v2 | ||
with: | ||
version: 7 | ||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: 16.x | ||
cache: "pnpm" | ||
|
||
- run: pnpm install --frozen-lockfile | ||
- name: Create Release Pull Request or Publish | ||
id: changesets | ||
uses: changesets/action@v1 | ||
with: | ||
publish: pnpm run release | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
NPM_TOKEN: ${{ secrets.NPM_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
auto-install-peers=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
pnpm-lock.yaml | ||
.next |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"semi": false, | ||
"singleQuote": true, | ||
"jsxSingleQuote": true, | ||
"arrowParens": "avoid" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
In the name of Allah | ||
|
||
# Overview | ||
|
||
await any prompt/modal with a `window.prompt`-like API + react hooks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
'use client' | ||
|
||
import { | ||
type FC, | ||
type JSX, | ||
type ReactNode, | ||
Fragment, | ||
createContext, | ||
useContext, | ||
useMemo, | ||
useRef, | ||
useState, | ||
} from 'react' | ||
|
||
interface PromptContext { | ||
allocSlot(getJsx: (id: string) => JSX.Element): string | ||
deleteSlot(id: string): void | ||
} | ||
|
||
const PromptContext = createContext<PromptContext | null>(null) | ||
|
||
export interface PromptContextProviderProps { | ||
children: ReactNode | ||
} | ||
|
||
export const PromptProvider: FC<PromptContextProviderProps> = ({ | ||
children, | ||
}) => { | ||
const [slots, setSlots] = useState<Slot[]>([]) | ||
const lastId = useRef(0) | ||
return ( | ||
<PromptContext.Provider | ||
value={useMemo( | ||
() => ({ | ||
allocSlot(getJsx) { | ||
const id = 'p' + lastId.current++ | ||
setSlots([...slots, { id, jsx: getJsx(id) }]) | ||
return id | ||
}, | ||
deleteSlot(id) { | ||
setSlots(slots.filter(s => s.id !== id)) | ||
}, | ||
}), | ||
[slots, setSlots] | ||
)} | ||
> | ||
{children} | ||
{slots.map(({ id, jsx }) => ( | ||
<Fragment key={id}>{jsx}</Fragment> | ||
))} | ||
</PromptContext.Provider> | ||
) | ||
} | ||
|
||
interface Slot { | ||
id: string | ||
jsx: JSX.Element | ||
} | ||
|
||
export default function usePrompt<ResolvesWith, RejectsWith, Variables>( | ||
PromptComponent: PromptComponent<ResolvesWith, RejectsWith, Variables> | ||
) { | ||
const { allocSlot, deleteSlot } = useContext(PromptContext) ?? {} | ||
return useMemo( | ||
() => ({ | ||
prompt: async (variables: Variables): Promise<ResolvesWith> => | ||
new Promise((resolve, reject) => { | ||
allocSlot?.(id => { | ||
const close = () => deleteSlot?.(id) | ||
return ( | ||
<PromptComponent | ||
{...{ variables }} | ||
resolve={value => { | ||
close() | ||
resolve(value) | ||
}} | ||
reject={error => { | ||
close() | ||
reject(error) | ||
}} | ||
/> | ||
) | ||
}) | ||
}), | ||
}), | ||
[PromptComponent, allocSlot, deleteSlot] | ||
) | ||
} | ||
|
||
export interface PromptComponentProps<ResolvesWith, RejectsWith, Variables> { | ||
resolve(v: ResolvesWith): void | ||
reject(v: RejectsWith): void | ||
variables: Variables | ||
} | ||
|
||
export type PromptComponent<ResolvesWith, RejectsWith, Variables> = FC< | ||
PromptComponentProps<ResolvesWith, RejectsWith, Variables> | ||
> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"name": "await-prompt", | ||
"description": "await any prompt/modal with a `window.prompt`-like API + react hooks", | ||
"author": "Arvin Nabavi", | ||
"license": "MIT", | ||
"version": "0.0.1", | ||
"main": "dist/index.js", | ||
"module": "dist/index.mjs", | ||
"types": "dist/index.d.ts", | ||
"keywords": [ | ||
"hook", | ||
"react", | ||
"prompt", | ||
"modal", | ||
"await", | ||
"async", | ||
"window.prompt", | ||
"usePrompt", | ||
"useModal" | ||
], | ||
"scripts": { | ||
"build": "tsup index.tsx --format cjs,esm --dts", | ||
"release": "pnpm run build && changeset publish", | ||
"lint": "tsc" | ||
}, | ||
"devDependencies": { | ||
"@changesets/cli": "^2.27.11", | ||
"@types/react": "^17.0.0 || ^18.0.0 || ^19.0.0", | ||
"tsup": "^8.3.5", | ||
"typescript": "^5.7.3" | ||
}, | ||
"peerDependencies": { | ||
"react": "^17.0.0 || ^18.0.0 || ^19.0.0" | ||
} | ||
} |
Oops, something went wrong.