Skip to content

Commit 1337f69

Browse files
authored
Expose a useDialogContext hook to dialog components (#174)
Co-authored-by: Alex Nicholson <alex.n@clove.kitchen>
1 parent 45ae091 commit 1337f69

12 files changed

Lines changed: 99 additions & 17 deletions
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
title: V2.5.0 has been released
3+
description: Release notes for version 2.5.0
4+
tags: [Release, Minor Release]
5+
---
6+
7+
Version 2.5.0 for React Dialog Async has been released 🎉
8+
<!-- truncate -->
9+
10+
## What's new?
11+
* Dialogs are now wrapped in a provider that exposes all of the props passed to the dialog component via react context. Call `useDialogContext` from a component inside a dialog to access all the dialog props such as `open`, `data`, and `handleClose`. [Read more about `useDialogContext`](/API/use-dialog-context).
12+
13+
---
14+
15+
Feedback or ideas? We'd love to hear them! Open an issue over on [GitHub](https://github.com/a16n-dev/react-dialog-async/issues).
16+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
sidebar_position: 3
3+
---
4+
5+
# useDialogContext
6+
```tsx
7+
import { useDialogContext } from 'react-dialog-async';
8+
```
9+
A hook that can be called from within a dialog component to access that dialog's state.
10+
11+
:::note
12+
Calling `useDialogContext` outside of a dialog component **will not throw an error**. This is an intentional design decision to allow for more flexible components. Instead check the value of `isInsideDialogContext` to determine if the hook is being called within a dialog.
13+
:::

docs/docs/API/use-dialog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
sidebar_position: 1
33
---
44

5-
# UseDialog
5+
# useDialog
66
```tsx
77
import { useDialog } from 'react-dialog-async';
88
```

packages/react-dialog-async/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "react-dialog-async",
33
"description": "A promise-based way to show dialogs in React",
44
"type": "module",
5-
"version": "2.4.0",
5+
"version": "2.5.0",
66
"sideEffects": false,
77
"main": "index.js",
88
"module": "index.esm.js",

packages/react-dialog-async/src/DialogContext.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { DialogComponent } from './types';
44
export interface dialogContextState {
55
show: (
66
dialogId: string,
7+
hash: number,
78
dialog: DialogComponent<any, any>,
89
data: unknown,
910
unmountDelay?: number,

packages/react-dialog-async/src/DialogProvider/DialogProvider.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ test('calling show() mounts the dialog in the DOM', () => {
3939
const id = useId();
4040

4141
useEffect(() => {
42-
ctx.show(id, TestDialog, {});
42+
ctx.show(id, 0, TestDialog, {});
4343
}, [id]);
4444

4545
return null;
@@ -61,7 +61,7 @@ test('calling register() followed by show() followed by hide() unmounts the dial
6161
const id = useId();
6262

6363
useEffect(() => {
64-
ctx.show(id, TestDialog, {});
64+
ctx.show(id, 0, TestDialog, {});
6565

6666
ctx.hide(id);
6767
}, [id]);

packages/react-dialog-async/src/DialogProvider/DialogProvider.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const DialogProvider = ({
6161
const show = useCallback(
6262
(
6363
id: string,
64+
hash: number,
6465
dialog: DialogComponent<any, any>,
6566
data: unknown,
6667
unmountDelay?: number,
@@ -83,6 +84,7 @@ const DialogProvider = ({
8384
[id]: {
8485
dialog: dialog,
8586
open: true,
87+
hash,
8688
data,
8789
resolve: resolveFn,
8890
unmountDelay: unmountDelay ?? defaultUnmountDelayInMs,

packages/react-dialog-async/src/DialogStateContext.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export type dialogsStateData = Record<
55
string,
66
{
77
dialog: DialogComponent<any, any>;
8+
hash: number;
89
data: unknown;
910
open: boolean;
1011
resolve?: (value?: unknown) => void;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { createContext, useContext } from 'react';
2+
import { AsyncDialogProps } from './types';
3+
4+
interface DialogContextValue<D = any, R = any> extends AsyncDialogProps<D, R> {
5+
isInsideDialogContext: boolean;
6+
}
7+
8+
const defaultDialogContextValue: DialogContextValue = {
9+
open: false,
10+
focused: false,
11+
mounted: true,
12+
handleClose: () => {},
13+
data: undefined,
14+
isInsideDialogContext: false,
15+
};
16+
17+
const IndividualDialogContext = createContext<DialogContextValue>(
18+
defaultDialogContextValue,
19+
);
20+
21+
export const useDialogContext = <D = any, R = any>(): DialogContextValue<D, R> => {
22+
return useContext(IndividualDialogContext);
23+
};
24+
25+
export default IndividualDialogContext;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export { default as DialogProvider } from './DialogProvider/DialogProvider';
22
export { default as DialogOutlet } from './DialogOutlet';
33
export { default as useDialog } from './useDialog';
4+
export { useDialogContext } from './IndividualDialogContext';
45
export { type AsyncDialogProps } from './types';

0 commit comments

Comments
 (0)