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(modal): add class on mount and fix backdrop #572

Merged
merged 7 commits into from
Sep 30, 2024
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ To locally test Atomium using Alpha/Beta versions, follow the steps below:

1. Update the `.release-please-manifest.json` file in the root directory of the Atomium project with the next version number + alpha. Ex: the current version is `2.10.0`, so the next alpha version can be `2.11.0-alpha.1` (OBS: in the example, it is updating only the core lib, update the libs that your changes impact).

![add alpha version to release](https://github.com/user-attachments/assets/3e3adaa2-1bcd-4442-8d1a-118cc14cc274)
![add alpha version to release](https://github.com/user-attachments/assets/91418116-266c-45f1-9cf2-bdf2d6c1a7eb)

2. Add the same version to the repespective `package.json` file in the root directory of the lib project. Ex: packages/core/package.json

![add alpha to package](https://github.com/user-attachments/assets/3e3adaa2-1bcd-4442-8d1a-118cc14cc274)
![add alpha to package](https://github.com/user-attachments/assets/ec4bfbf2-a822-4cf5-b7c4-66575fd36230)

3. Build the Atomium libraries by running the following command in the root directory of the Atomium project

Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export namespace Components {
"hasDivider": boolean;
"hasFooter": boolean;
"headerTitle": string;
"isOpen": boolean;
"primaryText"?: string;
"progress"?: number;
"secondaryText"?: string;
Expand Down Expand Up @@ -703,6 +704,7 @@ declare namespace LocalJSX {
"hasDivider"?: boolean;
"hasFooter"?: boolean;
"headerTitle"?: string;
"isOpen"?: boolean;
"onAtomCloseClick"?: (event: AtomModalCustomEvent<any>) => void;
"onAtomDidDismiss"?: (event: AtomModalCustomEvent<any>) => void;
"onAtomDidPresent"?: (event: AtomModalCustomEvent<any>) => void;
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/components/modal/modal.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
@import '~@atomium/scss-utils/index';

.atom-modal {
position: fixed;

&__close {
position: absolute;
right: var(--spacing-small);
Expand Down
122 changes: 122 additions & 0 deletions packages/core/src/components/modal/modal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@ import { newSpecPage, SpecPage } from '@stencil/core/testing'

import { AtomModal } from './modal'

Object.defineProperty(document.body, 'classList', {
value: {
add: jest.fn(),
remove: jest.fn(),
},
})

Object.defineProperty(document.documentElement, 'classList', {
value: {
add: jest.fn(),
remove: jest.fn(),
},
})

describe('atom-modal', () => {
let page: SpecPage

Expand Down Expand Up @@ -69,6 +83,114 @@ describe('atom-modal', () => {
expect(page.root?.textContent).toContain('Header from prop')
})

it('should render modal closed if is open is set to false', async () => {
page = await newSpecPage({
components: [AtomModal],
html: `
<atom-modal header-title="Header from prop" is-open="false">
Modal content
<div slot="header">Custom Header</div>
</atom-modal>
`,
})

expect(page.root?.innerHTML).not.toContain('isopen')
})
it('should render modal opened if is open is set to true', async () => {
page = await newSpecPage({
components: [AtomModal],
html: `
<atom-modal header-title="Header from prop" is-open="true">
Modal content
<div slot="header">Custom Header</div>
</atom-modal>
`,
})

expect(page.root?.innerHTML).toContain('isopen')

page.rootInstance.isOpen = false

await page.waitForChanges()

expect(document.body.classList.remove).toHaveBeenCalled()
})
it('should call remove and add on backdrop no scroll class when is-open changes', async () => {
page = await newSpecPage({
components: [AtomModal],
html: `
<atom-modal header-title="Header from prop" is-open="true">
Modal content
<div slot="header">Custom Header</div>
</atom-modal>
`,
})

expect(page.root?.innerHTML).toContain('isopen')

page.rootInstance.modal = {
dismiss: jest.fn(),
close: jest.fn(),
}
page.rootInstance.handleDidPresent()

expect(document.body.classList.add).toHaveBeenCalled()
expect(document.documentElement.classList.add).toHaveBeenCalled()

page.rootInstance.handleDidDismiss()

expect(page.rootInstance.modal.close).toHaveBeenCalled()

page.rootInstance.handleCloseClick()

expect(page.rootInstance.modal.close).toHaveBeenCalled()
})

it('should call remove and add on backdrop no scroll class when is-open changes', async () => {
page = await newSpecPage({
components: [AtomModal],
html: `
<atom-modal header-title="Header from prop" is-open="true">
Modal content
<div slot="header">Custom Header</div>
</atom-modal>
`,
})

expect(page.root?.innerHTML).toContain('isopen')

page.rootInstance.modal = {
dismiss: jest.fn(),
}

page.rootInstance.handleDidPresent()

expect(document.body.classList.add).toHaveBeenCalled()
expect(document.documentElement.classList.add).toHaveBeenCalled()
})

it('should call remove on class list when call remove classes', async () => {
page = await newSpecPage({
components: [AtomModal],
html: `
<atom-modal header-title="Header from prop" is-open="true">
Modal content
<div slot="header">Custom Header</div>
</atom-modal>
`,
})

page.rootInstance.addClasses()

expect(document.body.classList.add).toHaveBeenCalled()
expect(document.documentElement.classList.add).toHaveBeenCalled()

page.rootInstance.removeClasses()

expect(document.body.classList.remove).toHaveBeenCalled()
expect(document.documentElement.classList.remove).toHaveBeenCalled()
})

it('should render icon type when alertType is passed', async () => {
await page.setContent(`
<atom-modal alert-type="error">
Expand Down
44 changes: 31 additions & 13 deletions packages/core/src/components/modal/modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import { IconProps } from '../../icons'

type AlertType = Record<'alert' | 'error', { icon: IconProps; color: string }>

/* @todo it's needed to prevent a ionic error. In the version 8.0 it was fixed, remove it after the upgrade.
* https://github.com/ionic-team/ionic-framework/issues/23942
*/
const BACKDROP_NO_SCROLL = 'backdrop-no-scroll'

type HTMLAtomModalElement = HTMLIonModalElement & { close: () => Promise<void> }
export type HTMLAtomModalElement = HTMLIonModalElement & {
close: () => Promise<void>
}

@Component({
tag: 'atom-modal',
Expand All @@ -28,6 +27,7 @@ export class AtomModal {
@Prop() hasFooter = true
@Prop() disablePrimary = false
@Prop() disableSecondary = false
@Prop({ mutable: true }) isOpen = false

@Event() atomCloseClick: EventEmitter
@Event() atomDidDismiss: EventEmitter
Expand All @@ -37,7 +37,7 @@ export class AtomModal {

private modal: HTMLAtomModalElement

private alertMap: AlertType = {
private readonly alertMap: AlertType = {
alert: {
icon: 'alert-outline',
color: 'warning',
Expand All @@ -48,34 +48,50 @@ export class AtomModal {
},
}

private readonly addClasses = () => {
document.body.classList.add(BACKDROP_NO_SCROLL)
document.documentElement.classList.add(BACKDROP_NO_SCROLL)
}

private readonly removeClasses = () => {
document.body.classList.remove(BACKDROP_NO_SCROLL)
document.documentElement.classList.remove(BACKDROP_NO_SCROLL)
}

componentDidLoad() {
/* @todo it's needed to prevent a ionic error. In the version 8.0 it was fixed, remove it after the upgrade.
* https://github.com/ionic-team/ionic-framework/issues/23942
*/
document.body.classList.remove(BACKDROP_NO_SCROLL)

this.modal.close = async () => {
await this.modal.dismiss()

document.body.classList.remove(BACKDROP_NO_SCROLL)
this.removeClasses()
this.isOpen = false
}
}

private handleDidDimiss = () => {
private readonly handleDidDismiss = () => {
this.atomDidDismiss.emit(this.modal)
this.modal.close()
}

private handleDidPresent = () => {
private readonly handleDidPresent = () => {
this.atomDidPresent.emit(this.modal)
this.isOpen = true
this.addClasses()
}

private handleCloseClick = async () => {
private readonly handleCloseClick = async () => {
this.atomCloseClick.emit(this.modal)
this.modal.close()
}

private handleSecondaryClick = () => {
private readonly handleSecondaryClick = () => {
this.atomSecondaryClick.emit(this.modal)
}

private handlePrimaryClick = () => {
private readonly handlePrimaryClick = () => {
this.atomPrimaryClick.emit(this.modal)
}

Expand All @@ -93,8 +109,10 @@ export class AtomModal {
'atom-modal': true,
'atom-modal--progress': !!this.progress,
}}
onIonModalDidDismiss={this.handleDidDimiss}
onIonModalDidDismiss={this.handleDidDismiss}
onDidDismiss={this.handleDidDismiss}
onDidPresent={this.handleDidPresent}
isOpen={this.isOpen}
>
<header part='header' class='atom-modal__header'>
{iconType && (
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/components/modal/stories/modal.args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ export const ModalStoryArgs = {
category: Category.PROPERTIES,
},
},
isOpen: {
control: 'boolean',
description: 'If true, the modal will be opened. Default is false',
table: {
category: Category.PROPERTIES,
},
defaultValue: false,
},
disableSecondary: {
control: 'boolean',
description:
Expand Down Expand Up @@ -161,4 +169,5 @@ export const ModalComponentArgs = {
hasDivider: false,
disableSecondary: false,
disablePrimary: false,
isOpen: false,
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const createModal = (args) => {
header-title="${args.headerTitle}"
disable-secondary="${args.disableSecondary}"
disable-primary="${args.disablePrimary}"
is-open="${args.isOpen}"
>
<div slot="header">Custom Header</div>
<p>Modal Content</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const createModal = (args) => (
progress={args.progress}
disablePrimary={args.disablePrimary}
disableSecondary={args.disableSecondary}
isOpen={args.isOpen}
>
<div slot='header'>Custom Header</div>
<p>Modal Content</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const createModal = (args, themeColor = 'light') => ({
progress="${args.progress}"
disable-primary="${args.disablePrimary}"
disable-secondary="${args.disableSecondary}"
is-open="${args.isOpen}"
>
{{ args.label }}
</AtomModal>
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/global/global.scss
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,7 @@ Issue: https://github.com/ionic-team/ionic-framework/issues/27500
}
}
}

html.backdrop-no-scroll {
overflow: hidden;
}
Loading