-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create context with *use client* directive (#333)
- Loading branch information
1 parent
3b71d59
commit d266119
Showing
29 changed files
with
339 additions
and
261 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
strict-peer-dependencies=false | ||
enable-pre-post-scripts=true | ||
enable-pre-post-scripts=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
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 |
---|---|---|
@@ -1,26 +1,9 @@ | ||
import { Button } from '@openlabs/ui' | ||
import Playground from '@/components/playground' | ||
|
||
export default function Home() { | ||
return ( | ||
<Playground> | ||
<h1 className="text-xl font-extrabold uppercase">Zone game</h1> | ||
<div> | ||
------------------------------------- | ||
<br /> | ||
<small className="font-bold block text-center"> | ||
Down here is the playground | ||
</small> | ||
</div> | ||
{/* | ||
* The playground component is a wrapper that applies some styles to its children. This is a good example of how to create a reusable component in Next.js. | ||
Rules | ||
- Test UI components in isolation | ||
- Add the component to the page.tsx file | ||
- Remove the component from the page.tsx files | ||
*/} | ||
<Button>Hola</Button> | ||
</Playground> | ||
<main className="flex flex-col items-center justify-center w-screen h-screen"> | ||
<Button>Play</Button> | ||
</main> | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"name": "@openlabs/playground", | ||
"name": "playground", | ||
"version": "0.1.0", | ||
"private": true, | ||
"scripts": { | ||
|
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
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
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
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,52 @@ | ||
import * as React from 'react' | ||
|
||
export interface CreateContextOptions { | ||
/** | ||
* If `true`, React will throw if context is `null` or `undefined` | ||
* In some cases, you might want to support nested context, so you can set it to `false` | ||
*/ | ||
strict?: boolean | ||
/** | ||
* Error message to throw if the context is `undefined` | ||
*/ | ||
errorMessage?: string | ||
/** | ||
* The display name of the context | ||
*/ | ||
name?: string | ||
} | ||
|
||
export type CreateContextReturn<T> = [React.Provider<T>, () => T, React.Context<T>] | ||
|
||
/** | ||
* Creates a named context, provider, and hook. | ||
* | ||
* @param options create context options | ||
*/ | ||
export function createContext<ContextType>(options: CreateContextOptions = {}) { | ||
const { | ||
strict = true, | ||
errorMessage = 'useContext: `context` is undefined. Seems you forgot to wrap component within the Provider', | ||
name, | ||
} = options | ||
|
||
const Context = React.createContext<ContextType | undefined>(undefined) | ||
|
||
Context.displayName = name | ||
|
||
function useContext() { | ||
const context = React.useContext(Context) | ||
|
||
if (!context && strict) { | ||
const error = new Error(errorMessage) | ||
|
||
error.name = 'ContextError' | ||
Error.captureStackTrace?.(error, useContext) | ||
throw error | ||
} | ||
|
||
return context | ||
} | ||
|
||
return [Context.Provider, useContext, Context] as CreateContextReturn<ContextType> | ||
} |
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 @@ | ||
import { createContext } from './context' | ||
|
||
export interface ProviderContextProps {} | ||
|
||
export const [ProviderContext, useProviderContext] = createContext<ProviderContextProps>({ | ||
name: 'ProviderContext', | ||
strict: 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
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,14 @@ | ||
import type { ProviderContextProps } from './create-provider' | ||
import { ProviderContext } from './create-provider' | ||
|
||
export interface OpenUIProviderProps extends ProviderContextProps { | ||
children: React.ReactNode | ||
} | ||
|
||
export const OpenUIProvider: React.FC<OpenUIProviderProps> = ({ children }) => { | ||
return ( | ||
<ProviderContext value={{}}> | ||
{children} | ||
</ProviderContext> | ||
) | ||
} |
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
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,42 @@ | ||
import type * as React from 'react' | ||
import { forwardRef as baseForwardRef } from 'react' | ||
import type { | ||
As, | ||
InternalForwardRefRenderFunction, | ||
PropsOf, | ||
RightJoinProps, | ||
} from './types' | ||
|
||
export function forwardRef<Component extends As, Props extends object, OmitKeys extends keyof any = never>( | ||
component: React.ForwardRefRenderFunction<any, RightJoinProps<PropsOf<Component>, Props> & { as?: As }>, | ||
) { | ||
return baseForwardRef(component) as InternalForwardRefRenderFunction<Component, Props, OmitKeys> | ||
} | ||
|
||
export function toIterator(obj: any) { | ||
return { | ||
...obj, | ||
[Symbol.iterator]() { | ||
const keys = Object.keys(this) | ||
let index = 0 | ||
|
||
return { | ||
next: () => { | ||
if (index >= keys.length) { | ||
return { done: true } | ||
} | ||
const key = keys[index] | ||
const value = this[key] | ||
|
||
index++ | ||
|
||
return { value: { key, value }, done: false } | ||
}, | ||
} | ||
}, | ||
} | ||
} | ||
|
||
export function isOpenUIEl(component: React.ReactComponentElement<any>) { | ||
return !!component.type?.render?.displayName?.includes('OpenUI') | ||
} |
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 |
---|---|---|
@@ -1,11 +1,5 @@ | ||
{ | ||
"extends": "@openlabs/tsconfig/base.json", | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"paths": { | ||
"@system/types": ["types/index.d.ts"] | ||
} | ||
}, | ||
"extends": "@openlabs/tsconfig/react-lib.json", | ||
"include": ["."], | ||
"exclude": ["dist", "build", "node_modules"] | ||
} |
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
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
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
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
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
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
Oops, something went wrong.