Skip to content

Commit

Permalink
feat(sbb-dialog): introduce backdrop property to control backdrop d…
Browse files Browse the repository at this point in the history
…ensity
  • Loading branch information
jeripeierSBB committed Dec 23, 2024
1 parent 9d47116 commit f91e56d
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
export const snapshots = {};

snapshots["sbb-dialog renders an open dialog DOM"] =
`<sbb-dialog data-state="opened">
`<sbb-dialog
backdrop="opaque"
data-state="opened"
>
<sbb-dialog-title
aria-level="2"
level="2"
Expand Down
4 changes: 4 additions & 0 deletions src/elements/dialog/dialog/dialog.scss
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
}
}

:host([backdrop='translucent']:is([data-state='opened'], [data-state='opening'])) {
--sbb-dialog-backdrop-color: var(--sbb-color-black-alpha-50);
}

:host([data-hide-header]) {
// Hide transition
--sbb-dialog-header-margin-block-start: calc(var(--sbb-dialog-header-height) * -1);
Expand Down
18 changes: 18 additions & 0 deletions src/elements/dialog/dialog/dialog.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ const accessibilityBackLabel: InputType = {
},
};

const backdrop: InputType = {
control: {
type: 'inline-radio',
},
options: ['opaque', 'translucent'],
};

const backdropAction: InputType = {
control: {
type: 'select',
Expand All @@ -99,6 +106,7 @@ const basicArgTypes: ArgTypes = {
accessibilityBackLabel,
negative,
'accessibility-label': accessibilityLabel,
backdrop: backdrop,
'backdrop-action': backdropAction,
};

Expand All @@ -110,6 +118,7 @@ const basicArgs: Args = {
accessibilityBackLabel: 'Go back',
negative: false,
'accessibility-label': undefined,
backdrop: 'opaque',
'backdrop-action': backdropAction.options![0],
};

Expand Down Expand Up @@ -391,6 +400,15 @@ export const Negative: StoryObj = {
},
};

export const TranslucentBackdrop: StoryObj = {
render: DefaultTemplate,
argTypes: basicArgTypes,
args: {
...basicArgs,
backdrop: 'translucent',
},
};

export const AllowBackdropClick: StoryObj = {
render: DefaultTemplate,
argTypes: basicArgTypes,
Expand Down
5 changes: 5 additions & 0 deletions src/elements/dialog/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class SbbDialogElement extends SbbOverlayBaseElement {
@property({ attribute: 'backdrop-action' }) public accessor backdropAction: 'close' | 'none' =
'close';

/** Backdrop density. */
@property({ attribute: 'backdrop', reflect: true }) public accessor backdrop:
| 'opaque'
| 'translucent' = 'opaque';

// We use a timeout as a workaround to the "ResizeObserver loop completed with undelivered notifications" error.
// For more details:
// - https://github.com/WICG/resize-observer/issues/38#issuecomment-422126006
Expand Down
18 changes: 17 additions & 1 deletion src/elements/dialog/dialog/dialog.visual.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe(`sbb-dialog`, () => {
const dialogContent = (longContent = false): TemplateResult => html`
<sbb-dialog-content>
<p style="margin: 0">
What really knocks me out is a book that, when you're all done reading it, you wish the
What really knocks me out is a book that, when you're all done reading it, you wish the
author that wrote it was a terrific friend of yours and you could call him up on the phone
whenever you felt like it.
</p>
Expand Down Expand Up @@ -112,5 +112,21 @@ describe(`sbb-dialog`, () => {
setup.withPostSetupAction(() => dialog.open());
}),
);

it(
`backdrop=translucent`,
visualDiffDefault.with(async (setup) => {
await setup.withFixture(html`
<p>Other content visible in the background</p>
<sbb-dialog backdrop="translucent">
${dialogTitle()} ${dialogContent()} ${dialogFooter()}
</sbb-dialog>
`);
const dialog = setup.snapshotElement.querySelector('sbb-dialog')!;
setup.withSnapshotElement(dialog);

setup.withPostSetupAction(() => dialog.open());
}),
);
});
});
13 changes: 7 additions & 6 deletions src/elements/dialog/dialog/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,13 @@ The `sbb-dialog` component may visually hide the title thanks to the `hideOnScro

## Properties

| Name | Attribute | Privacy | Type | Default | Description |
| -------------------- | --------------------- | ------- | ------------------- | --------- | ----------------------------------------------------------------------------------------------------------- |
| `accessibilityLabel` | `accessibility-label` | public | `string` | `''` | This will be forwarded as aria-label to the relevant nested element to describe the purpose of the overlay. |
| `backdropAction` | `backdrop-action` | public | `'close' \| 'none'` | `'close'` | Backdrop click action. |
| `isOpen` | - | public | `boolean` | | Whether the element is open. |
| `negative` | `negative` | public | `boolean` | `false` | Negative coloring variant flag. |
| Name | Attribute | Privacy | Type | Default | Description |
| -------------------- | --------------------- | ------- | --------------------------- | ---------- | ----------------------------------------------------------------------------------------------------------- |
| `accessibilityLabel` | `accessibility-label` | public | `string` | `''` | This will be forwarded as aria-label to the relevant nested element to describe the purpose of the overlay. |
| `backdrop` | `backdrop` | public | `'opaque' \| 'translucent'` | `'opaque'` | Backdrop density. |
| `backdropAction` | `backdrop-action` | public | `'close' \| 'none'` | `'close'` | Backdrop click action. |
| `isOpen` | - | public | `boolean` | | Whether the element is open. |
| `negative` | `negative` | public | `boolean` | `false` | Negative coloring variant flag. |

## Methods

Expand Down

0 comments on commit f91e56d

Please sign in to comment.