Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
arvin-nabavi committed Jan 9, 2025
0 parents commit 5f64cbc
Show file tree
Hide file tree
Showing 15 changed files with 2,085 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .changeset/README.md
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)
11 changes: 11 additions & 0 deletions .changeset/config.json
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": []
}
5 changes: 5 additions & 0 deletions .changeset/fast-goats-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"my-package": patch
---

Added the add function
12 changes: 12 additions & 0 deletions .editorconfig
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
21 changes: 21 additions & 0 deletions .github/workflows/main.yml
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
36 changes: 36 additions & 0 deletions .github/workflows/publish.yml
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 }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
auto-install-peers=true
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
pnpm-lock.yaml
.next
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"semi": false,
"singleQuote": true,
"jsxSingleQuote": true,
"arrowParens": "avoid"
}
5 changes: 5 additions & 0 deletions README.md
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
98 changes: 98 additions & 0 deletions index.tsx
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>
>
35 changes: 35 additions & 0 deletions package.json
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"
}
}
Loading

0 comments on commit 5f64cbc

Please sign in to comment.