Skip to content

Commit

Permalink
Move ariaLabelledBy to container
Browse files Browse the repository at this point in the history
  • Loading branch information
Hinton committed Sep 5, 2022
1 parent 90bc673 commit 09ae825
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 45 deletions.
31 changes: 31 additions & 0 deletions libs/components/src/dialog/dialog-title.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { CdkDialogContainer, DialogRef } from "@angular/cdk/dialog";
import { Directive, HostBinding, Input, OnInit, Optional } from "@angular/core";

// Increments for each instance of this component
let nextId = 0;

@Directive({
selector: "[bitDialogTitle]",
})
export class DialogTitleDirective implements OnInit {
@HostBinding("class") get classList() {
return ["tw-mb-0", "tw-grow", "tw-text-lg", "tw-uppercase"];
}
@HostBinding("id") id = `bitDialogTitle-${nextId++}`;

@Input("bitDialogClose") dialogResult: any;

constructor(@Optional() private dialogRef: DialogRef<any>) {}

ngOnInit(): void {
if (this.dialogRef) {
Promise.resolve().then(() => {
const container = this.dialogRef.containerInstance as CdkDialogContainer;

if (container && !container._ariaLabelledBy) {
container._ariaLabelledBy = this.id;
}
});
}
}
}
10 changes: 8 additions & 2 deletions libs/components/src/dialog/dialog.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ import { CommonModule } from "@angular/common";
import { NgModule } from "@angular/core";

import { DialogCloseDirective } from "./dialog-close.directive";
import { DialogTitleDirective } from "./dialog-title.directive";
import { DialogService } from "./dialog.service";
import { DialogComponent } from "./dialog/dialog.component";
import { SimpleDialogComponent } from "./simple-dialog/simple-dialog.component";

@NgModule({
imports: [CommonModule, CdkDialogModule],
declarations: [DialogCloseDirective, DialogComponent, SimpleDialogComponent],
exports: [CdkDialogModule, DialogComponent, SimpleDialogComponent],
declarations: [
DialogCloseDirective,
DialogComponent,
DialogTitleDirective,
SimpleDialogComponent,
],
exports: [CdkDialogModule, DialogComponent, DialogTitleDirective, SimpleDialogComponent],
providers: [DialogService],
})
export class DialogModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Meta, moduleMetadata, Story } from "@storybook/angular";
import { ButtonModule } from "../button";

import { DialogCloseDirective } from "./dialog-close.directive";
import { DialogTitleDirective } from "./dialog-title.directive";
import { DialogService } from "./dialog.service";
import { DialogComponent } from "./dialog/dialog.component";

Expand Down Expand Up @@ -32,13 +33,13 @@ class StoryDialogComponent {
selector: "story-dialog-content",
template: `
<bit-dialog [dialogSize]="large">
<span bit-dialog-title>Dialog Title</span>
<span bit-dialog-content>
<h2 bitDialogTitle>Dialog Title</h2>
<span bitDialogContent>
Dialog body text goes here.
<br />
Animal: {{ animal }}
</span>
<div bit-dialog-footer class="tw-flex tw-flex-row tw-gap-2">
<div bitDialogFooter class="tw-flex tw-flex-row tw-gap-2">
<button bitButton buttonType="primary" (click)="dialogRef.close()">Save</button>
<button bitButton buttonType="secondary" bitDialogClose>Cancel</button>
</div>
Expand All @@ -58,7 +59,12 @@ export default {
component: StoryDialogComponent,
decorators: [
moduleMetadata({
declarations: [DialogComponent, StoryDialogContentComponent, DialogCloseDirective],
declarations: [
DialogCloseDirective,
DialogComponent,
DialogTitleDirective,
StoryDialogContentComponent,
],
imports: [ButtonModule, DialogModule],
providers: [DialogService],
}),
Expand Down
8 changes: 3 additions & 5 deletions libs/components/src/dialog/dialog/dialog.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@
<div
class="tw-flex tw-gap-4 tw-border-0 tw-border-b tw-border-solid tw-border-secondary-300 tw-p-4"
>
<h2 [id]="dialogTitleId" class="tw-mb-0 tw-grow tw-text-lg tw-uppercase">
<ng-content select="[bit-dialog-title]"></ng-content>
</h2>
<ng-content select="[bitDialogTitle]"></ng-content>
<button class="tw-border-0 tw-bg-transparent tw-p-0" bitDialogClose>
<i class="bwi bwi-close tw-text-xs tw-font-bold tw-text-main" aria-hidden="true"></i>
</button>
</div>

<div class="tw-overflow-y-auto tw-p-4 tw-pb-8">
<ng-content select="[bit-dialog-content]"></ng-content>
<ng-content select="[bitDialogContent]"></ng-content>
</div>

<div
class="tw-border-0 tw-border-t tw-border-solid tw-border-secondary-300 tw-bg-background-alt tw-p-4"
>
<ng-content select="[bit-dialog-footer]"></ng-content>
<ng-content select="[bitDialogFooter]"></ng-content>
</div>
</div>
8 changes: 1 addition & 7 deletions libs/components/src/dialog/dialog/dialog.component.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { Component, HostBinding, Input } from "@angular/core";

// Increments for each instance of this component
let nextId = 0;
import { Component, Input } from "@angular/core";

@Component({
selector: "bit-dialog",
templateUrl: "./dialog.component.html",
})
export class DialogComponent {
@HostBinding() role = "dialog";
@HostBinding("attr.aria-labelledby") dialogTitleId = `bit-dialog-title-${nextId++}`;

@Input() dialogSize: "small" | "default" | "large" = "default";

get width() {
Expand Down
14 changes: 8 additions & 6 deletions libs/components/src/dialog/dialog/dialog.stories.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Meta, moduleMetadata, Story } from "@storybook/angular";

import { ButtonModule } from "../../button";
import { DialogTitleDirective } from "../dialog-title.directive";

import { DialogComponent } from "./dialog.component";

Expand All @@ -10,6 +11,7 @@ export default {
decorators: [
moduleMetadata({
imports: [ButtonModule],
declarations: [DialogTitleDirective],
}),
],
args: {
Expand All @@ -27,9 +29,9 @@ const Template: Story<DialogComponent> = (args: DialogComponent) => ({
props: args,
template: `
<bit-dialog [dialogSize]="dialogSize">
<span bit-dialog-title>{{title}}</span>
<span bit-dialog-content>Dialog body text goes here.</span>
<div bit-dialog-footer class="tw-flex tw-flex-row tw-gap-2">
<h2 bitDialogTitle>{{title}}</h2>
<span bitDialogContent>Dialog body text goes here.</span>
<div bitDialogFooter class="tw-flex tw-flex-row tw-gap-2">
<button bitButton buttonType="primary">Save</button>
<button bitButton buttonType="secondary">Cancel</button>
</div>
Expand Down Expand Up @@ -59,15 +61,15 @@ const TemplateScrolling: Story<DialogComponent> = (args: DialogComponent) => ({
props: args,
template: `
<bit-dialog [dialogSize]="dialogSize">
<span bit-dialog-title>Scrolling Example</span>
<span bit-dialog-content>
<span bitDialogTitle>Scrolling Example</span>
<span bitDialogContent>
Dialog body text goes here.<br>
<ng-container *ngFor="let _ of [].constructor(100)">
repeating lines of characters <br>
</ng-container>
end of sequence!
</span>
<div bit-dialog-footer class="tw-flex tw-flex-row tw-gap-2">
<div bitDialogFooter class="tw-flex tw-flex-row tw-gap-2">
<button bitButton buttonType="primary">Save</button>
<button bitButton buttonType="secondary">Cancel</button>
</div>
Expand Down
84 changes: 84 additions & 0 deletions libs/components/src/dialog/simple-dialog.service.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { DialogModule, DialogRef, DIALOG_DATA } from "@angular/cdk/dialog";
import { Component, Inject } from "@angular/core";
import { Meta, moduleMetadata, Story } from "@storybook/angular";

import { ButtonModule } from "../button";

import { DialogCloseDirective } from "./dialog-close.directive";
import { DialogTitleDirective } from "./dialog-title.directive";
import { DialogService } from "./dialog.service";
import { SimpleDialogComponent } from "./simple-dialog/simple-dialog.component";

interface Animal {
animal: string;
}

@Component({
selector: "app-story-dialog",
template: `<button bitButton (click)="openDialog()">Open Simple Dialog</button>`,
})
class StoryDialogComponent {
constructor(public dialogService: DialogService) {}

openDialog() {
this.dialogService.open(StoryDialogContentComponent, {
data: {
animal: "panda",
},
});
}
}

@Component({
selector: "story-dialog-content",
template: `
<bit-simple-dialog>
<h2 bitDialogTitle>Dialog Title</h2>
<span bitDialogContent>
Dialog body text goes here.
<br />
Animal: {{ animal }}
</span>
<div bitDialogFooter class="tw-flex tw-flex-row tw-gap-2">
<button bitButton buttonType="primary" (click)="dialogRef.close()">Save</button>
<button bitButton buttonType="secondary" bitDialogClose>Cancel</button>
</div>
</bit-simple-dialog>
`,
})
class StoryDialogContentComponent {
constructor(public dialogRef: DialogRef, @Inject(DIALOG_DATA) private data: Animal) {}

get animal() {
return this.data?.animal;
}
}

export default {
title: "Component Library/Dialogs/Service/Simple",
component: StoryDialogComponent,
decorators: [
moduleMetadata({
declarations: [
DialogCloseDirective,
SimpleDialogComponent,
DialogTitleDirective,
StoryDialogContentComponent,
],
imports: [ButtonModule, DialogModule],
providers: [DialogService],
}),
],
parameters: {
design: {
type: "figma",
url: "https://www.figma.com/file/Zt3YSeb6E6lebAffrNLa0h/Tailwind-Component-Library",
},
},
} as Meta;

const Template: Story<StoryDialogComponent> = (args: StoryDialogComponent) => ({
props: args,
});

export const Default = Template.bind({});
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
<ng-template #elseBlock>
<i class="bwi bwi-exclamation-triangle tw-text-3xl tw-text-warning" aria-hidden="true"></i>
</ng-template>
<h2 [id]="dialogTitleId" class="tw-mb-0 tw-text-base tw-font-semibold">
<ng-content select="[bit-dialog-title]"></ng-content>
</h2>
<ng-content select="[bitDialogTitle]"></ng-content>
</div>
<div class="tw-overflow-y-auto tw-px-4 tw-pt-2 tw-pb-4 tw-text-center tw-text-base">
<ng-content select="[bit-dialog-content]"></ng-content>
<ng-content select="[bitDialogContent]"></ng-content>
</div>
<div class="tw-border-0 tw-border-t tw-border-solid tw-border-secondary-300 tw-p-4">
<ng-content select="[bit-dialog-footer]"></ng-content>
<ng-content select="[bitDialogFooter]"></ng-content>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { Component, ContentChild, Directive, HostBinding } from "@angular/core";

// Increments for each instance of this component
let nextId = 0;
import { Component, ContentChild, Directive } from "@angular/core";

@Directive({ selector: "[bit-dialog-icon]" })
export class IconDirective {}
Expand All @@ -11,9 +8,6 @@ export class IconDirective {}
templateUrl: "./simple-dialog.component.html",
})
export class SimpleDialogComponent {
@HostBinding() role = "dialog";
@HostBinding("attr.aria-labelledby") dialogTitleId = `bit-simple-dialog-title-${nextId++}`;

@ContentChild(IconDirective) icon!: IconDirective;

get hasIcon() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ const Template: Story<SimpleDialogComponent> = (args: SimpleDialogComponent) =>
props: args,
template: `
<bit-simple-dialog>
<span bit-dialog-title>Alert Dialog</span>
<span bit-dialog-content>Message Content</span>
<div bit-dialog-footer class="tw-flex tw-flex-row tw-gap-2">
<span bitDialogTitle>Alert Dialog</span>
<span bitDialogContent>Message Content</span>
<div bitDialogFooter class="tw-flex tw-flex-row tw-gap-2">
<button bitButton buttonType="primary">Yes</button>
<button bitButton buttonType="secondary">No</button>
</div>
Expand All @@ -42,9 +42,9 @@ const TemplateWithIcon: Story<SimpleDialogComponent> = (args: SimpleDialogCompon
template: `
<bit-simple-dialog>
<i bit-dialog-icon class="bwi bwi-star tw-text-3xl tw-text-success" aria-hidden="true"></i>
<span bit-dialog-title>Premium Subscription Available</span>
<span bit-dialog-content> Message Content</span>
<div bit-dialog-footer class="tw-flex tw-flex-row tw-gap-2">
<span bitDialogTitle>Premium Subscription Available</span>
<span bitDialogContent> Message Content</span>
<div bitDialogFooter class="tw-flex tw-flex-row tw-gap-2">
<button bitButton buttonType="primary">Yes</button>
<button bitButton buttonType="secondary">No</button>
</div>
Expand All @@ -58,16 +58,16 @@ const TemplateScroll: Story<SimpleDialogComponent> = (args: SimpleDialogComponen
props: args,
template: `
<bit-simple-dialog>
<span bit-dialog-title>Alert Dialog</span>
<span bit-dialog-content>
<span bitDialogTitle>Alert Dialog</span>
<span bitDialogContent>
Message Content
Message text goes here.<br>
<ng-container *ngFor="let _ of [].constructor(100)">
repeating lines of characters <br>
</ng-container>
end of sequence!
</span>
<div bit-dialog-footer class="tw-flex tw-flex-row tw-gap-2">
<div bitDialogFooter class="tw-flex tw-flex-row tw-gap-2">
<button bitButton buttonType="primary">Yes</button>
<button bitButton buttonType="secondary">No</button>
</div>
Expand Down

0 comments on commit 09ae825

Please sign in to comment.