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

Fix/2773 dialog v3 cherry pick #3232

Merged
merged 2 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 13 additions & 0 deletions packages/react/src/component-tests/IcDialog/IcDialog.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
NoBackgroundClickDialog,
SimpleDialog,
SlottedContentDialog,
SlottedUpdatedContentDialog,
NoHeightConstraintDialog,
AlertDialog,
SizeDialog,
Expand Down Expand Up @@ -81,6 +82,18 @@ describe("IcDialog end-to-end tests", () => {
cy.get("ic-button#test-button").should(HAVE_FOCUS);
});

it("should focus interactive content added after first load - including children of slotted elements", () => {
mount(<SlottedUpdatedContentDialog />);

cy.get(DIALOG).should("exist");
cy.get("ic-button#display-btn-1-btn").click().wait(300);
cy.get("ic-button#display-btn-2-btn").click();
cy.get("ic-button#display-dialog-btn").click().wait(300);
cy.get(DIALOG).should(HAVE_ATTR, "open");
cy.get("ic-button#test-button-1").should(HAVE_FOCUS).realPress("Tab");
cy.get("ic-button#test-button-2").should(HAVE_FOCUS);
});

it("should not hide dialog on background click when background click is disabled", () => {
mount(<NoBackgroundClickDialog />);

Expand Down
32 changes: 32 additions & 0 deletions packages/react/src/component-tests/IcDialog/IcDialogTestData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,38 @@ export const SlottedContentDialog = () => {
);
};

export const SlottedUpdatedContentDialog = () => {
const [showBtn1, setShowBtn1] = useState(false);
const [showBtn2, setShowBtn2] = useState(false);
const dialogEl = useRef<any>(null);
const handleClick = () => {
dialogEl.current.open = true;
};
return (
<>
<IcButton id="display-btn-1-btn" onClick={() => setShowBtn1(true)}>
Display button 1
</IcButton>
<IcButton id="display-btn-2-btn" onClick={() => setShowBtn2(true)}>
Display button 2
</IcButton>
<IcButton id="display-dialog-btn" onClick={handleClick}>
Display dialog
</IcButton>
<IcDialog
ref={dialogEl}
heading="This is a default dialog"
label="Dialog"
>
<div>
{showBtn1 && <IcButton id="test-button-1">Test button 1</IcButton>}
</div>
{showBtn2 && <IcButton id="test-button-2">Test button 2</IcButton>}
</IcDialog>
</>
);
};

export const DialogAccordion = () => {
const dialogEl = useRef<any>(null);
const handleClick = () => {
Expand Down
6 changes: 6 additions & 0 deletions packages/react/src/stories/ic-dialog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ import * as IcDialogStories from './ic-dialog.stories';
<Story of={IcDialogStories.Popover} />
</Canvas>

### Show / hide interactive elements

<Canvas>
<Story of={IcDialogStories.ShowHideInteractiveElements} />
</Canvas>

### Hidden close button

<Canvas>
Expand Down
124 changes: 63 additions & 61 deletions packages/react/src/stories/ic-dialog.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,66 +35,6 @@ const showLargeDialog = () => {
dialog.open = true;
};

const DialogContent = () => {
const [show, setShow] = useState(false);
const handleShow = (show) => {
setShow(show);
};
return (
<>
<IcTypography>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua.
</IcTypography>
<IcButton onClick={() => handleShow(true)}>Show</IcButton>
<IcButton onClick={() => handleShow(false)}>Hide</IcButton>
{show && (
<>
<IcButton>Tab Here</IcButton>
<IcTypography>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Lorem
ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Lorem ipsum
dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Lorem ipsum dolor sit
amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Lorem ipsum dolor sit amet,
consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Lorem ipsum dolor sit amet,
consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Lorem ipsum dolor sit amet,
consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Lorem ipsum dolor sit amet,
consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Lorem ipsum dolor sit amet,
consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Lorem ipsum dolor sit amet,
consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Lorem ipsum dolor sit amet,
consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Lorem ipsum dolor sit amet,
consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Lorem ipsum dolor sit amet,
consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Lorem ipsum dolor sit amet,
consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Lorem ipsum dolor sit amet,
consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Lorem ipsum dolor sit amet,
consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Lorem ipsum dolor sit amet,
consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Lorem ipsum dolor sit amet,
consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua.
</IcTypography>
</>
)}
</>
);
};

const showDialog = () => {
const dialog = document.querySelector("ic-dialog");
dialog.open = true;
Expand Down Expand Up @@ -149,6 +89,49 @@ const dropdownItems = () => {
});
};

const showShowHideContentDialog = () => {
const dialog = document.querySelector("#show-hide-content-dialog");
dialog.open = true;
};

const ShowHideContent = () => {
const [showEl1, setShowEl1] = useState(false);
const [showEl2, setShowEl2] = useState(false);
const handleShow = (show) => {
// Delay prevents false positive by ensuring the two slot updates happen at separate times
setTimeout(() => {
setShowEl1(show);
}, 2000);
setShowEl2(show);
};
return (
<>
<IcTypography>
Demonstrates changes to slotted elements happening after first load.
<br />
The button which is a child of an existing slotted element will update after a 2s delay.
</IcTypography>
<br />
<IcButton onClick={() => handleShow(true)}>Show</IcButton>
<IcButton onClick={() => handleShow(false)}>Hide</IcButton>
<div>
{showEl1 && (
<IcButton>Child of slotted element</IcButton>
)}
</div>
{showEl2 && (
<>
<IcButton>Slotted element</IcButton>
<IcTypography>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.
</IcTypography>
</>
)}
</>
);
};

const showHiddenCloseButtonDialog = () => {
const dialog = document.querySelector("#hidden-close-button-dialog");
dialog.open = true;
Expand Down Expand Up @@ -203,7 +186,10 @@ export const Sizes = {
size="medium"
id="medium-dialog"
>
<DialogContent />
<IcTypography>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua.
</IcTypography>
</IcDialog>
<IcDialog
heading="This is a large dialog"
Expand Down Expand Up @@ -544,6 +530,22 @@ export const Popover = {
name: "Popover",
};

export const ShowHideInteractiveElements = {
render: () => (
<>
<IcButton onClick={showShowHideContentDialog}>Launch show / hide content dialog</IcButton>
<IcDialog
id="show-hide-content-dialog"
heading="This dialog allows showing and hiding of content"
>
<ShowHideContent />
</IcDialog>
</>
),

name: "Show / hide interactive elements",
}

export const HiddenCloseButton = {
render: () => (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import * as IcDialogStories from "./ic-dialog.stories";
<Story of={IcDialogStories.DisableWidthConstraint} />
</Canvas>

### Show/hide interactive elements
### Show / hide interactive elements

<Canvas>
<Story of={IcDialogStories.ShowHideInteractiveElements} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -911,40 +911,56 @@ export const DisableWidthConstraint = {
export const ShowHideInteractiveElements = {
render: () => html`
<script>
function showSmallDialog() {
dialog = document.querySelector("#small-dialog").open = true;
function showShowHideContentDialog() {
dialog = document.querySelector(
"#show-hide-content-dialog"
).open = true;
}
var btnShow = document.querySelector(".show");
btnShow.addEventListener("click", () => {
var el = document.createElement("ic-button");
el.id = "toggleBtn";
el.innerText = "Hello World!";
el.id = "slotted-btn";
el.innerText = "Slotted element";
var childEl = document.createElement("ic-button");
childEl.id = "slotted-child-btn";
childEl.innerText = "Child of slotted element";
var base = document.querySelector("#base");
base.after(el);
setTimeout(() => base.appendChild(childEl), 2000);
});
var btnHide = document.querySelector(".hide");
btnHide.addEventListener("click", () => {
if (document.querySelector("#toggleBtn")) {
var btn = document.querySelector("#toggleBtn");
if (document.querySelector("#slotted-btn")) {
var btn = document.querySelector("#slotted-btn");
btn.remove();
}
if (document.querySelector("#slotted-child-btn")) {
var btn = document.querySelector("#slotted-child-btn");
setTimeout(() => btn.remove(), 2000);
}
});
</script>
<ic-button variant="primary" onclick="showSmallDialog()"
>Launch small dialog</ic-button
<ic-button variant="primary" onclick="showShowHideContentDialog()"
>Launch show / hide content dialog</ic-button
>
<ic-dialog
heading="This dialog allows showing and hiding of content"
id="show-hide-content-dialog"
>
<ic-dialog heading="This is a small dialog" label="Small" id="small-dialog">
<ic-typography>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua.
Demonstrates changes to slotted elements happening after first load.
<br />
The button which is a child of an existing slotted element will update
after a 2s delay.
</ic-typography>
<br />
<ic-button class="show">Show</ic-button>
<ic-button class="hide">Hide</ic-button>
<div id="base"></div>
</ic-dialog>
`,

name: "show/hide interactive elements",
name: "Show / hide interactive elements",
};

export const HiddenCloseButton = {
Expand Down
26 changes: 22 additions & 4 deletions packages/web-components/src/components/ic-dialog/ic-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
isSlotUsed,
checkResizeObserver,
onComponentRequiredPropUndefined,
getSlotElements,
} from "../../utils/helpers";
import { IcThemeMode } from "../../utils/types";

Expand All @@ -33,6 +34,7 @@ import { IcThemeMode } from "../../utils/types";
export class Dialog {
private backdropEl: HTMLDivElement;
private contentArea: HTMLSlotElement;
private contentAreaMutationObserver: MutationObserver = null;
private DATA_GETS_FOCUS: string = "data-gets-focus";
private DATA_GETS_FOCUS_SELECTOR: string = "[data-gets-focus]";
private DIALOG_CONTROLS: string = "dialog-controls";
Expand Down Expand Up @@ -288,12 +290,26 @@ export class Dialog {
};

private refreshInteractiveElementsOnSlotChange = () => {
this.contentArea = this.el.shadowRoot.querySelector("#dialog-content slot");
const contentWrapper = this.el.shadowRoot.querySelector("#dialog-content");
this.contentArea = contentWrapper.querySelector("slot");

// Detect changes to slotted elements
this.contentArea.addEventListener(
"slotchange",
this.getInteractiveElements
);

this.contentAreaMutationObserver = new MutationObserver(() => {
this.getInteractiveElements();
});

// Detect changes to children of slotted elements
getSlotElements(contentWrapper).forEach((el) => {
this.contentAreaMutationObserver.observe(el, {
childList: true,
subtree: true,
});
});
};

private removeSlotChangeListener = () => {
Expand All @@ -302,6 +318,8 @@ export class Dialog {
"slotchange",
this.getInteractiveElements
);

this.contentAreaMutationObserver?.disconnect();
}
};

Expand Down Expand Up @@ -353,9 +371,9 @@ export class Dialog {
);
const slottedInteractiveElements = Array.from(
this.el.querySelectorAll(
`a[href], button, input:not(.ic-input), textarea, select, details, [tabindex]:not([tabindex="-1"]),
ic-button, ic-checkbox, ic-select, ic-search-bar, ic-tab-group, ic-radio-group,
ic-back-to-top, ic-breadcrumb, ic-chip[dismissible="true"], ic-footer-link, ic-link, ic-navigation-button,
`a[href], button, input:not(.ic-input), textarea, select, details, [tabindex]:not([tabindex="-1"]),
ic-button, ic-checkbox, ic-select, ic-search-bar, ic-tab-group, ic-radio-group,
ic-back-to-top, ic-breadcrumb, ic-chip[dismissible="true"], ic-footer-link, ic-link, ic-navigation-button,
ic-navigation-item, ic-switch, ic-text-field, ic-accordion-group, ic-accordion, ic-date-input, ic-date-picker`
)
);
Expand Down
Loading