Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add centralized dialog management #2489

Merged
merged 33 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
9751e15
feat: add centralized dialog management
MartinCupela Sep 4, 2024
eb6ab99
Merge branch 'release-v11' into feat/dialogs-manager
MartinCupela Sep 5, 2024
143555b
feat: use own anchor root for DialogAnchor
MartinCupela Sep 5, 2024
c3ed317
feat: forward custom DialogsManager id via DialogsManagerProvider
MartinCupela Sep 5, 2024
71c7685
feat: apply dialogs manager to message lists only
MartinCupela Sep 5, 2024
8e48860
fix: do not forward prop mine to MessageActionsBox root div
MartinCupela Sep 5, 2024
223ddea
test: fix MessageActions tests
MartinCupela Sep 5, 2024
d45459b
test: fix tests rendering message actions
MartinCupela Sep 5, 2024
ba89493
feat: assign static id to DialogsManager inside MessageList
MartinCupela Sep 5, 2024
993358f
docs: fix todo comment
MartinCupela Sep 5, 2024
b557e25
feat: handle focus within dialog
MartinCupela Sep 5, 2024
0a14548
feat: prevent rendering dialog contents if not open
MartinCupela Sep 6, 2024
07eb261
refactor: do not register event listeners by MessageActions component
MartinCupela Sep 6, 2024
d9f3709
test: open MessageActionsBox first in MessageActionsBox.test.js
MartinCupela Sep 6, 2024
af6b94b
feat: control ReactionsSelector dialog display
MartinCupela Sep 6, 2024
8d1c98a
fix: close MessageActionsBox on click inside
MartinCupela Sep 9, 2024
9f071e4
test: add DialogManager tests
MartinCupela Sep 9, 2024
9d1ee9d
refactor: unmound popper element if closed in useDialogAnchor
MartinCupela Sep 9, 2024
86b4cb0
Merge branch 'feat/dialogs-manager'
MartinCupela Sep 9, 2024
d1e4df4
refactor: use StateStore to handle DialogsManager subscriptions
MartinCupela Sep 10, 2024
f76cc75
refactor: rename DialogsManager to DialogManager
MartinCupela Sep 11, 2024
cb52d15
refactor: rename DialogsManager.open param single to closeRest
MartinCupela Sep 11, 2024
45c5eb7
refactor: rename DialogsManager.state.dialogs to DialogsManager.state…
MartinCupela Sep 11, 2024
401af81
refactor: move useStateStore to src/store/hooks
MartinCupela Sep 11, 2024
4f120a4
refactor: remove openDialogCount from DialogManager
MartinCupela Sep 11, 2024
a344533
refactor: apply suggestions about declaring state selector
MartinCupela Sep 11, 2024
1447022
Merge branch 'master' into feat/dialog-manager
MartinCupela Sep 11, 2024
a212d27
fix: remove unsupported onClick prop from ReactionListProps
MartinCupela Sep 11, 2024
9932001
docs: remove references to removed props
MartinCupela Sep 11, 2024
7b32ea3
docs: add dialog management guide and migration guide
MartinCupela Sep 11, 2024
f649140
chore(docs): bump stream-chat-css to v5.0.0-rc.6
MartinCupela Sep 13, 2024
7a67c03
refactor: merge Dialog.toggleOpen and Dialog.toggleOpenSingle into Di…
MartinCupela Sep 16, 2024
580b2bf
Merge branch 'master' into feat/dialog-manager
MartinCupela Sep 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docusaurus/docs/React/guides/theming/message-ui.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ const CustomMessageUi = () => {

Message grouping is being managed automatically by the SDK and each parent element (which holds our message UI) receives an appropriate class name based on which we can adjust our rules to display metadata elements only when it's appropriate to make our UI look less busy.

{/_ TODO: link to grouping logic (maybe how to adjust it if needed) _/}
[//]: # 'TODO: link to grouping logic (maybe how to adjust it if needed)'

```css
.custom-message-ui__metadata {
Expand Down
116 changes: 116 additions & 0 deletions src/components/Dialog/DialogAnchor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import clsx from 'clsx';
import { Placement } from '@popperjs/core';
import React, { ComponentProps, PropsWithChildren, useEffect, useState } from 'react';
import { FocusScope } from '@react-aria/focus';
import { usePopper } from 'react-popper';
import { DialogPortalEntry } from './DialogPortal';
import { useDialog, useDialogIsOpen } from './hooks';

export interface DialogAnchorOptions {
open: boolean;
placement: Placement;
referenceElement: HTMLElement | null;
}

export function useDialogAnchor<T extends HTMLElement>({
open,
placement,
referenceElement,
}: DialogAnchorOptions) {
const [popperElement, setPopperElement] = useState<T | null>(null);
const { attributes, styles, update } = usePopper(referenceElement, popperElement, {
modifiers: [
{
name: 'eventListeners',
options: {
// It's not safe to update popper position on resize and scroll, since popper's
// reference element might not be visible at the time.
resize: false,
scroll: false,
},
},
],
placement,
});

useEffect(() => {
if (open && popperElement) {
// Since the popper's reference element might not be (and usually is not) visible
// all the time, it's safer to force popper update before showing it.
// update is non-null only if popperElement is non-null
update?.();
}
}, [open, popperElement, update]);

if (popperElement && !open) {
setPopperElement(null);
}

return {
attributes,
setPopperElement,
styles,
};
}

type DialogAnchorProps = PropsWithChildren<Partial<DialogAnchorOptions>> & {
id: string;
focus?: boolean;
trapFocus?: boolean;
} & ComponentProps<'div'>;

export const DialogAnchor = ({
children,
className,
focus = true,
id,
placement = 'auto',
referenceElement = null,
trapFocus,
...restDivProps
}: DialogAnchorProps) => {
const dialog = useDialog({ id });
const open = useDialogIsOpen(id);
const { attributes, setPopperElement, styles } = useDialogAnchor<HTMLDivElement>({
open,
placement,
referenceElement,
});

useEffect(() => {
if (!open) return;
const hideOnEscape = (event: KeyboardEvent) => {
if (event.key !== 'Escape') return;
dialog?.close();
};

document.addEventListener('keyup', hideOnEscape);

return () => {
document.removeEventListener('keyup', hideOnEscape);
};
}, [dialog, open]);

// prevent rendering the dialog contents if the dialog should not be open / shown
if (!open) {
return null;
}

return (
<DialogPortalEntry dialogId={id}>
<FocusScope autoFocus={focus} contain={trapFocus} restoreFocus>
<div
{...restDivProps}
{...attributes.popper}
className={clsx('str-chat__dialog-contents', className)}
data-testid='str-chat__dialog-contents'
ref={setPopperElement}
style={styles.popper}
tabIndex={0}
>
{children}
</div>
</FocusScope>
</DialogPortalEntry>
);
};
60 changes: 60 additions & 0 deletions src/components/Dialog/DialogPortal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import React, { PropsWithChildren, useEffect, useLayoutEffect, useState } from 'react';
import { createPortal } from 'react-dom';
import { useDialogIsOpen } from './hooks';
import { useDialogsManager } from '../../context';

export const DialogPortalDestination = () => {
const { dialogsManager } = useDialogsManager();
const [shouldRender, setShouldRender] = useState(
!!dialogsManager.state.getLatestValue().openDialogCount,
);

useEffect(
() =>
dialogsManager.state.subscribeWithSelector<number[]>(
({ openDialogCount }) => [openDialogCount],
([openDialogCount]) => {
setShouldRender(openDialogCount > 0);
},
),
[dialogsManager],
);

return (
<div
className='str-chat__dialog-overlay'
data-str-chat__portal-id={dialogsManager.id}
data-testid='str-chat__dialog-overlay'
onClick={() => dialogsManager.closeAll()}
style={
{
'--str-chat__dialog-overlay-height': shouldRender ? '100%' : '0',
} as React.CSSProperties
}
></div>
);
};

type DialogPortalEntryProps = {
dialogId: string;
};

export const DialogPortalEntry = ({
children,
dialogId,
}: PropsWithChildren<DialogPortalEntryProps>) => {
const { dialogsManager } = useDialogsManager();
const dialogIsOpen = useDialogIsOpen(dialogId);
const [portalDestination, setPortalDestination] = useState<Element | null>(null);
useLayoutEffect(() => {
const destination = document.querySelector(
`div[data-str-chat__portal-id="${dialogsManager.id}"]`,
);
if (!destination) return;
setPortalDestination(destination);
}, [dialogsManager, dialogIsOpen]);

if (!portalDestination) return null;

return createPortal(children, portalDestination);
};
133 changes: 133 additions & 0 deletions src/components/Dialog/DialogsManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { StateStore } from 'stream-chat';

type DialogId = string;

export type GetOrCreateParams = {
id: DialogId;
};

export type Dialog = {
close: () => void;
id: DialogId;
isOpen: boolean | undefined;
open: (zIndex?: number) => void;
remove: () => void;
toggle: () => void;
toggleSingle: () => void;
};

type DialogInitOptions = {
id?: string;
};

type Dialogs = Record<DialogId, Dialog>;

type DialogsManagerState = {
dialogs: Dialogs;
openDialogCount: number;
};

MartinCupela marked this conversation as resolved.
Show resolved Hide resolved
export class DialogsManager {
MartinCupela marked this conversation as resolved.
Show resolved Hide resolved
id: string;
state = new StateStore<DialogsManagerState>({
dialogs: {},
MartinCupela marked this conversation as resolved.
Show resolved Hide resolved
openDialogCount: 0,
});

constructor({ id }: DialogInitOptions = {}) {
this.id = id ?? new Date().getTime().toString();
}

getOrCreate({ id }: GetOrCreateParams) {
let dialog = this.state.getLatestValue().dialogs[id];
if (!dialog) {
dialog = {
close: () => {
this.close(id);
},
id,
isOpen: false,
open: () => {
this.open({ id });

Check warning on line 51 in src/components/Dialog/DialogsManager.ts

View check run for this annotation

Codecov / codecov/patch

src/components/Dialog/DialogsManager.ts#L51

Added line #L51 was not covered by tests
},
remove: () => {
this.remove(id);

Check warning on line 54 in src/components/Dialog/DialogsManager.ts

View check run for this annotation

Codecov / codecov/patch

src/components/Dialog/DialogsManager.ts#L54

Added line #L54 was not covered by tests
},
toggle: () => {
this.toggleOpen({ id });

Check warning on line 57 in src/components/Dialog/DialogsManager.ts

View check run for this annotation

Codecov / codecov/patch

src/components/Dialog/DialogsManager.ts#L57

Added line #L57 was not covered by tests
},
toggleSingle: () => {
this.toggleOpenSingle({ id });
},
};
this.state.next((current) => ({
...current,
...{ dialogs: { ...current.dialogs, [id]: dialog } },
}));
}
return dialog;
}

open(params: GetOrCreateParams, single?: boolean) {
MartinCupela marked this conversation as resolved.
Show resolved Hide resolved
const dialog = this.getOrCreate(params);
if (dialog.isOpen) return;
if (single) {
this.closeAll();
}
this.state.next((current) => ({
...current,
dialogs: { ...current.dialogs, [dialog.id]: { ...dialog, isOpen: true } },
openDialogCount: ++current.openDialogCount,
}));
}

close(id: DialogId) {
const dialog = this.state.getLatestValue().dialogs[id];
if (!dialog?.isOpen) return;
dialog.isOpen = false;
MartinCupela marked this conversation as resolved.
Show resolved Hide resolved
this.state.next((current) => ({
...current,
dialogs: { ...current.dialogs, [dialog.id]: { ...dialog, isOpen: false } },
openDialogCount: --current.openDialogCount,
}));
}

closeAll() {
Object.values(this.state.getLatestValue().dialogs).forEach((dialog) => dialog.close());
}

toggleOpen(params: GetOrCreateParams) {
if (this.state.getLatestValue().dialogs[params.id]?.isOpen) {
this.close(params.id);
} else {
this.open(params);
}
}

toggleOpenSingle(params: GetOrCreateParams) {
if (this.state.getLatestValue().dialogs[params.id]?.isOpen) {
this.close(params.id);
} else {
this.open(params, true);
}
}

remove(id: DialogId) {
const state = this.state.getLatestValue();
const dialog = state.dialogs[id];
if (!dialog) return;

this.state.next((current) => ({
...current,
dialogs: Object.entries(current.dialogs).reduce<Dialogs>((acc, [dialogId, dialog]) => {
if (id !== dialogId) {
acc[id] = dialog;
}
return acc;
}, {}),
openDialogCount:
current.openDialogCount &&
(dialog.isOpen ? current.openDialogCount - 1 : current.openDialogCount),
}));
MartinCupela marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading
Loading