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

[Popover2] Add option to remove keyboard close button #3027

Merged
merged 2 commits into from
Aug 20, 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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<div class="textField-input-affix">
<button
[luPopover2]="popoverMultilanguage"
luPopoverNoCloseButton
[customPositions]="popoverPositions"
[luTooltip]="intl.toggleMultilanguage"
type="button"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<div class="popover" (cdkObserveContent)="contentChanged()" [debounce]="contentChangedDebounceTime">
@if (!config.noCloseButton) {
<div>
<button type="button" luButton class="popover-close" (click)="close()">
<lu-icon icon="signClose"></lu-icon>
<span class="u-mask">{{ intl.close }}</span>
</button>
</div>

}
<ng-container *ngTemplateOutlet="content"></ng-container>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ export class PopoverContentComponent implements AfterViewInit {

#elementRef = inject<ElementRef<HTMLElement>>(ElementRef);

#config = inject(POPOVER_CONFIG);
config = inject(POPOVER_CONFIG);

destroyRef = inject(DestroyRef);

@HostBinding('attr.id')
contentId = this.#config.contentId;
contentId = this.config.contentId;

content: TemplateRef<unknown> = this.#config.content;
content: TemplateRef<unknown> = this.config.content;

#focusManager = new PopoverFocusTrap(this.#elementRef.nativeElement, this.#config.triggerElement);
#focusManager = new PopoverFocusTrap(this.#elementRef.nativeElement, this.config.triggerElement);

closed$ = new Subject<void>();

Expand All @@ -54,12 +54,12 @@ export class PopoverContentComponent implements AfterViewInit {
}

contentChanged() {
this.#config.ref.updatePosition();
this.config.ref.updatePosition();
}

ngAfterViewInit(): void {
this.#focusManager.attachAnchors();
if (!this.#config.disableFocusManipulation) {
if (!this.config.disableFocusManipulation) {
void this.#focusManager.focusInitialElementWhenReady();
}
}
Expand All @@ -70,16 +70,16 @@ export class PopoverContentComponent implements AfterViewInit {

@HostListener('window:keydown.escape')
close(): void {
if (!this.#config.disableFocusManipulation) {
if (!this.config.disableFocusManipulation) {
// Focus initial trigger element
this.#config.triggerElement.focus();
this.config.triggerElement.focus();
}
// Tell the directive we're closed now
this.closed$.next();
this.closed$.complete();
this.mouseEnter$.complete();
this.mouseLeave$.complete();
// Detach overlay
this.#config.ref.detach();
this.config.ref.detach();
}
}
1 change: 1 addition & 0 deletions packages/ng/popover2/popover-tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface PopoverConfig {
ref: OverlayRef;
contentId: string;
disableFocusManipulation: boolean;
noCloseButton: boolean;
}

export const POPOVER_CONFIG = new InjectionToken<PopoverConfig>('Popover:Config');
7 changes: 7 additions & 0 deletions packages/ng/popover2/popover.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ export class PopoverDirective implements OnDestroy {
@Input()
customPositions?: ConnectionPositionPair[];

@Input({ transform: booleanAttribute })
/**
* Removes close button entirely, this is bad for a11y but sometimes we want it.
*/
luPopoverNoCloseButton = false;

// We have to type these two for Compodoc to find the right type and tell Storybook these aren't strings
luPopoverOpenDelay: InputSignal<number> = input<number>(300);

Expand Down Expand Up @@ -222,6 +228,7 @@ export class PopoverDirective implements OnDestroy {
contentId: this.ariaControls,
triggerElement: this.#elementRef.nativeElement,
disableFocusManipulation: disableFocusHandler,
noCloseButton: this.luPopoverNoCloseButton,
};
this.#componentRef = this.#overlayRef.attach(
new ComponentPortal(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@ export const Basic: StoryObj<PopoverDirective> = {
luPopoverOpenDelay: 300,
luPopoverDisabled: false,
luPopoverPosition: 'above',
luPopoverNoCloseButton: false,
},
};