-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from cal-smith/master
feat: add overflow menu and associated components
- Loading branch information
Showing
10 changed files
with
238 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/dialog/overflow-menu/overflow-menu-option.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { | ||
HostBinding, | ||
Component, | ||
Input, | ||
ElementRef | ||
} from "@angular/core"; | ||
|
||
/** | ||
* `OverflowMenuOption` represents a single option in an overflow menu | ||
* | ||
* Presently it has three possible states - normal, disabled, and danger: | ||
* ``` | ||
* <ibm-overflow-menu-option>Simple option</ibm-overflow-menu-option> | ||
* <ibm-overflow-menu-option disabled="true">Disabled</ibm-overflow-menu-option> | ||
* <ibm-overflow-menu-option type="danger">Danger option</ibm-overflow-menu-option> | ||
* ``` | ||
* | ||
* For content that expands beyod the overflow menu `OverflowMenuOption` automatically adds a title attribute. | ||
*/ | ||
@Component({ | ||
selector: "ibm-overflow-menu-option", | ||
template: ` | ||
<button | ||
class="bx--overflow-menu-options__btn" | ||
[ngClass]="{ | ||
'bx--overflow-menu-options__option--danger': type === 'danger', | ||
'bx--overflow-menu-options__option--disabled': disabled | ||
}" | ||
[tabindex]="(disabled ? -1 : null)" | ||
[title]="(titleEnabled ? content : '')"> | ||
<ng-content></ng-content> | ||
</button> | ||
` | ||
}) | ||
export class OverflowMenuOption { | ||
@HostBinding("class") optionClass = "bx--overflow-menu-options__option"; | ||
@HostBinding("attr.role") role = "list-item"; | ||
|
||
/** | ||
* toggles between `normal` and `danger` states | ||
*/ | ||
@Input() type: "normal" | "danger" = "normal"; | ||
/** | ||
* disable/enable interactions | ||
*/ | ||
@Input() disabled = false; | ||
|
||
constructor(private elementRef: ElementRef) {} | ||
|
||
/** | ||
* Returns true if the content string is longer than the width of the containing button | ||
* | ||
* note: getter ties into the view check cycle so we always get an accurate value | ||
*/ | ||
get titleEnabled() { | ||
const button = this.elementRef.nativeElement.querySelector("button"); | ||
if (button.scrollWidth > button.offsetWidth) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Returns the text content projected into the component | ||
*/ | ||
get content(): string { | ||
return this.elementRef.nativeElement.querySelector("button").textContent; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Component } from "@angular/core"; | ||
import { Dialog } from "../dialog.component"; | ||
|
||
/** | ||
* Extend the `Dialog` component to create an overflow menu. | ||
* | ||
* Not used directly. See overflow-menu.component and overflow-menu.directive for more | ||
*/ | ||
@Component({ | ||
selector: "ibm-overflow-menu-pane", | ||
template: ` | ||
<div #dialog> | ||
<ul | ||
class="bx--overflow-menu-options bx--overflow-menu-options--open" | ||
tabindex="-1"> | ||
<ng-template | ||
[ngTemplateOutlet]="dialogConfig.content" | ||
[ngTemplateOutletContext]="{overflowMenu: this}"> | ||
</ng-template> | ||
</ul> | ||
</div> | ||
` | ||
}) | ||
export class OverflowMenuPane extends Dialog {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Component } from "@angular/core"; | ||
|
||
/** | ||
* The OverFlow menu component encapsulates the OverFlowMenu directive, and the menu iconography into one convienent component | ||
* | ||
* html: | ||
* ``` | ||
* <ibm-overflow-menu [options]="overflowContent"></ibm-overflow-menu> | ||
* <ng-template #overflowContent> | ||
* <ibm-overflow-menu-option>Option 1</ibm-overflow-menu-option> | ||
* <ibm-overflow-menu-option>Option 2</ibm-overflow-menu-option> | ||
* </ng-template> | ||
* ``` | ||
*/ | ||
@Component({ | ||
selector: "ibm-overflow-menu", | ||
template: ` | ||
<div | ||
[ibmOverflowMenu]="options" | ||
class="bx--overflow-menu" | ||
style="display: block;"> | ||
<svg class="bx--overflow-menu__icon" width="3" height="15" viewBox="0 0 3 15"> | ||
<g fill-rule="evenodd"> | ||
<circle cx="1.5" cy="1.5" r="1.5" /> | ||
<circle cx="1.5" cy="7.5" r="1.5" /> | ||
<circle cx="1.5" cy="13.5" r="1.5" /> | ||
</g> | ||
</svg> | ||
</div> | ||
<ng-template #options> | ||
<ng-content></ng-content> | ||
</ng-template> | ||
` | ||
}) | ||
export class OverflowMenu {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { | ||
Directive, | ||
ElementRef, | ||
ViewContainerRef, | ||
Input, | ||
TemplateRef | ||
} from "@angular/core"; | ||
import { DialogDirective } from "./../dialog.directive"; | ||
import { DialogService } from "./../dialog.service"; | ||
import { OverflowMenuPane } from "./overflow-menu-pane.component"; | ||
|
||
|
||
/** | ||
* Directive for extending `Dialog` to create overflow menus. | ||
* | ||
* class: OverflowMenuDirective (extends DialogDirective) | ||
* | ||
* | ||
* selector: `ibmOverflowMenu` | ||
* | ||
* | ||
* ```html | ||
* <div [ibmOverflowMenu]="templateRef"></div> | ||
* <ng-template #templateRef> | ||
* <!-- overflow menu options here --> | ||
* </ng-template> | ||
* ``` | ||
*/ | ||
@Directive({ | ||
selector: "[ibmOverflowMenu]", | ||
exportAs: "ibmOverflowMenu", | ||
providers: [ | ||
DialogService | ||
] | ||
}) | ||
export class OverflowMenuDirective extends DialogDirective { | ||
@Input() ibmOverflowMenu: TemplateRef<any>; | ||
|
||
/** | ||
* Creates an instance of `OverflowMenuDirective`. | ||
*/ | ||
constructor( | ||
protected elementRef: ElementRef, | ||
protected viewContainerRef: ViewContainerRef, | ||
protected dialogService: DialogService | ||
) { | ||
super(elementRef, viewContainerRef, dialogService); | ||
dialogService.create(OverflowMenuPane); | ||
} | ||
|
||
onDialogInit() { | ||
this.dialogConfig.content = this.ibmOverflowMenu; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { storiesOf, moduleMetadata } from "@storybook/angular"; | ||
|
||
import { TranslateModule } from "@ngx-translate/core"; | ||
|
||
import { DialogModule } from "../../"; | ||
|
||
storiesOf("Overflow Menu", module) | ||
.addDecorator( | ||
moduleMetadata({ | ||
imports: [ | ||
DialogModule, | ||
TranslateModule.forRoot() | ||
] | ||
}) | ||
) | ||
.add("Basic", () => ({ | ||
template: ` | ||
<ibm-overflow-menu> | ||
<ibm-overflow-menu-option> | ||
An example option that is really long to show what should be done to handle long text | ||
</ibm-overflow-menu-option> | ||
<ibm-overflow-menu-option>Option 2</ibm-overflow-menu-option> | ||
<li class="bx--overflow-menu-options__option"> | ||
<button class="bx--overflow-menu-options__btn">A fully custom option</button> | ||
</li> | ||
<ibm-overflow-menu-option>Option 4</ibm-overflow-menu-option> | ||
<ibm-overflow-menu-option disabled="true">Disabled</ibm-overflow-menu-option> | ||
<ibm-overflow-menu-option type="danger">Danger option</ibm-overflow-menu-option> | ||
</ibm-overflow-menu> | ||
<ibm-dialog-placeholder></ibm-dialog-placeholder> | ||
` | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.