Skip to content

Commit

Permalink
feat(core): add dropdown offsets to TUI_DROPDOWN_OPTIONS
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladyslav Bondar committed Dec 23, 2022
1 parent 8826a20 commit d1ad281
Show file tree
Hide file tree
Showing 21 changed files with 52 additions and 37 deletions.
13 changes: 7 additions & 6 deletions projects/core/directives/dropdown/dropdown-options.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@ import {
} from '@taiga-ui/core/types';
import {tuiOverrideOptions} from '@taiga-ui/core/utils';

/**
* Safe space around host and screen edges
*/
export const TUI_DROPDOWN_OFFSET = 4;

export interface TuiDropdownOptions {
readonly align: TuiHorizontalDirection;
readonly direction: TuiVerticalDirection | null;
readonly limitWidth: TuiDropdownWidth;
readonly minHeight: number;
readonly maxHeight: number;
readonly offset: number;
}

/** Default values for dropdown options */
Expand All @@ -36,6 +32,7 @@ export const TUI_DROPDOWN_DEFAULT_OPTIONS: TuiDropdownOptions = {
limitWidth: `auto`,
maxHeight: 400,
minHeight: 80,
offset: 25,
};

export const TUI_DROPDOWN_OPTIONS = new InjectionToken<TuiDropdownOptions>(
Expand All @@ -57,7 +54,7 @@ export const tuiDropdownOptionsProvider: (
});

@Directive({
selector: `[tuiDropdownAlign], [tuiDropdownDirection], [tuiDropdownLimitWidth], [tuiDropdownMinHeight], [tuiDropdownMaxHeight]`,
selector: `[tuiDropdownAlign], [tuiDropdownDirection], [tuiDropdownLimitWidth], [tuiDropdownMinHeight], [tuiDropdownMaxHeight], [tuiDropdownOffset]`,
providers: [
{
provide: TUI_DROPDOWN_OPTIONS,
Expand Down Expand Up @@ -86,6 +83,10 @@ export class TuiDropdownOptionsDirective implements TuiDropdownOptions {
@tuiDefaultProp()
maxHeight = this.options.maxHeight;

@Input(`tuiDropdownOffset`)
@tuiDefaultProp()
offset = this.options.offset;

constructor(
@SkipSelf()
@Inject(TUI_DROPDOWN_OPTIONS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import {
} from '@taiga-ui/core/abstract';
import {TuiPoint} from '@taiga-ui/core/types';

import {
TUI_DROPDOWN_OFFSET,
TUI_DROPDOWN_OPTIONS,
TuiDropdownOptions,
} from './dropdown-options.directive';
import {TUI_DROPDOWN_OPTIONS, TuiDropdownOptions} from './dropdown-options.directive';
import {TuiDropdownPositionDirective} from './dropdown-position.directive';

@Directive({
Expand Down Expand Up @@ -46,15 +42,15 @@ export class TuiDropdownPositionSidedDirective implements TuiPositionAccessor {
const {align, direction, minHeight} = this.options;
const available = {
top: hostRect.bottom,
left: hostRect.left - TUI_DROPDOWN_OFFSET,
right: innerWidth - hostRect.right - TUI_DROPDOWN_OFFSET,
left: hostRect.left - this.options.offset,
right: innerWidth - hostRect.right - this.options.offset,
bottom: innerHeight - hostRect.top,
} as const;
const position = {
top: hostRect.bottom - height + 2 * TUI_DROPDOWN_OFFSET + 1, // 1 for border
left: hostRect.left - width - TUI_DROPDOWN_OFFSET,
right: hostRect.right + TUI_DROPDOWN_OFFSET,
bottom: hostRect.top - 2 * TUI_DROPDOWN_OFFSET - 1, // 1 for border
top: hostRect.bottom - height + 1, // 1 for border
left: hostRect.left - width - this.options.offset,
right: hostRect.right + this.options.offset,
bottom: hostRect.top - 1, // 1 for border
} as const;
const better = available.top > available.bottom ? `top` : `bottom`;
const maxLeft = available.left > available.right ? position.left : position.right;
Expand Down
18 changes: 7 additions & 11 deletions projects/core/directives/dropdown/dropdown-position.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import {
} from '@taiga-ui/core/abstract';
import {TuiPoint, TuiVerticalDirection} from '@taiga-ui/core/types';

import {
TUI_DROPDOWN_OFFSET,
TUI_DROPDOWN_OPTIONS,
TuiDropdownOptions,
} from './dropdown-options.directive';
import {TUI_DROPDOWN_OPTIONS, TuiDropdownOptions} from './dropdown-options.directive';

@Directive({
selector: `[tuiDropdown]:not([tuiDropdownCustomPosition]):not([tuiDropdownSided])`,
Expand All @@ -31,17 +27,17 @@ export class TuiDropdownPositionDirective implements TuiPositionAccessor {
const {innerHeight, innerWidth} = this.windowRef;
const {minHeight, align, direction} = this.options;
const previous = this.previous || direction || `bottom`;
const right = Math.max(hostRect.right - width, TUI_DROPDOWN_OFFSET);
const right = Math.max(hostRect.right - width, this.options.offset);
const available = {
top: hostRect.top - 2 * TUI_DROPDOWN_OFFSET,
bottom: innerHeight - hostRect.bottom - 2 * TUI_DROPDOWN_OFFSET,
top: hostRect.top - 2 * this.options.offset,
bottom: innerHeight - hostRect.bottom - 2 * this.options.offset,
} as const;
const position = {
top: hostRect.top - TUI_DROPDOWN_OFFSET - height,
bottom: hostRect.bottom + TUI_DROPDOWN_OFFSET,
top: hostRect.top - this.options.offset - height,
bottom: hostRect.bottom + this.options.offset,
right,
left:
hostRect.left + width < innerWidth - TUI_DROPDOWN_OFFSET
hostRect.left + width < innerWidth - this.options.offset
? hostRect.left
: right,
} as const;
Expand Down
14 changes: 5 additions & 9 deletions projects/core/directives/dropdown/dropdown.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ import {takeUntil} from 'rxjs/operators';
// eslint-disable-next-line import/no-cycle
import {TuiDropdownDirective} from './dropdown.directive';
import {TuiDropdownHoverDirective} from './dropdown-hover.directive';
import {
TUI_DROPDOWN_OFFSET,
TUI_DROPDOWN_OPTIONS,
TuiDropdownOptions,
} from './dropdown-options.directive';
import {TUI_DROPDOWN_OPTIONS, TuiDropdownOptions} from './dropdown-options.directive';

/**
* This component is used to show template in a portal using default style of white rounded box with a shadow
Expand Down Expand Up @@ -105,13 +101,13 @@ export class TuiDropdownComponent {
const isIntersecting =
left < rect.right &&
right > rect.left &&
top < offsetY + 2 * TUI_DROPDOWN_OFFSET;
top < offsetY + 2 * this.options.offset;
const available = isIntersecting
? rect.top - 2 * TUI_DROPDOWN_OFFSET
: offsetY + innerHeight - top - TUI_DROPDOWN_OFFSET;
? rect.top - 2 * this.options.offset
: offsetY + innerHeight - top - this.options.offset;

style.position = position;
style.top = tuiPx(Math.max(top, offsetY + TUI_DROPDOWN_OFFSET));
style.top = tuiPx(Math.max(top, offsetY + this.options.offset));
style.left = tuiPx(left);
style.maxHeight = tuiPx(Math.min(maxHeight, available));
style.width = ``;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const DROPDOWN_CONTROLLER_SELECTORS = [
`tuiDropdownLimitWidth`,
`tuiDropdownMinHeight`,
`tuiDropdownMaxHeight`,
`tuiDropdownOffset`,
];

@Component({
Expand Down
2 changes: 2 additions & 0 deletions projects/demo/src/modules/components/abstract/control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ export abstract class AbstractExampleTuiControl

postfix = this.prefixVariants[0];

dropdownOffset = TUI_DROPDOWN_DEFAULT_OPTIONS.offset;

get customContent(): PolymorpheusContent {
return this.customContentSelected === CUSTOM_SVG_NAME
? CUSTOM_SVG
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,13 @@
>
Maximum height of dropdown
</ng-template>
<ng-template
i18n
documentationPropertyName="dropdownOffset"
documentationPropertyMode="input"
documentationPropertyType="number"
[(documentationPropertyValue)]="documentedComponent.dropdownOffset"
>
Dropdown offset
</ng-template>
</tui-doc-documentation>
1 change: 1 addition & 0 deletions projects/demo/src/modules/components/abstract/dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ export abstract class AbstractExampleTuiDropdown {
dropdownLimitWidth = TUI_DROPDOWN_DEFAULT_OPTIONS.limitWidth;
dropdownMinHeight = TUI_DROPDOWN_DEFAULT_OPTIONS.minHeight;
dropdownMaxHeight = TUI_DROPDOWN_DEFAULT_OPTIONS.maxHeight;
dropdownOffset = TUI_DROPDOWN_DEFAULT_OPTIONS.offset;
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
[tuiDropdownLimitWidth]="dropdownLimitWidth"
[tuiDropdownMinHeight]="dropdownMinHeight"
[tuiDropdownMaxHeight]="dropdownMaxHeight"
[tuiDropdownOffset]="dropdownOffset"
[tuiHintContent]="hintContent"
[tuiHintDirection]="hintDirection"
[tuiHintAppearance]="hintAppearance"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
[tuiDropdownLimitWidth]="dropdownLimitWidth"
[tuiDropdownMinHeight]="dropdownMinHeight"
[tuiDropdownMaxHeight]="dropdownMaxHeight"
[tuiDropdownOffset]="dropdownOffset"
[(open)]="open"
>
<tui-input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
[tuiDropdownDirection]="dropdownDirection"
[tuiDropdownMinHeight]="dropdownMinHeight"
[tuiDropdownMaxHeight]="dropdownMaxHeight"
[tuiDropdownOffset]="dropdownOffset"
[tuiHintContent]="hintContent"
[tuiHintDirection]="hintDirection"
[tuiHintAppearance]="hintAppearance"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
[tuiDropdownLimitWidth]="dropdownLimitWidth"
[tuiDropdownMinHeight]="dropdownMinHeight"
[tuiDropdownMaxHeight]="dropdownMaxHeight"
[tuiDropdownOffset]="dropdownOffset"
[tuiHintContent]="hintContent"
[tuiHintDirection]="hintDirection"
[tuiHintAppearance]="hintAppearance"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
[tuiDropdownLimitWidth]="dropdownLimitWidth"
[tuiDropdownMinHeight]="dropdownMinHeight"
[tuiDropdownMaxHeight]="dropdownMaxHeight"
[tuiDropdownOffset]="dropdownOffset"
[tuiHintContent]="hintContent"
[tuiHintDirection]="hintDirection"
[tuiHintAppearance]="hintAppearance"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
[tuiDropdownLimitWidth]="dropdownLimitWidth"
[tuiDropdownMinHeight]="dropdownMinHeight"
[tuiDropdownMaxHeight]="dropdownMaxHeight"
[tuiDropdownOffset]="dropdownOffset"
[items]="items"
[itemSize]="itemSize"
[tuiTextfieldCleaner]="cleaner"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@
[tuiDropdownLimitWidth]="dropdownLimitWidth"
[tuiDropdownMinHeight]="dropdownMinHeight"
[tuiDropdownMaxHeight]="dropdownMaxHeight"
[tuiDropdownOffset]="dropdownOffset"
[tuiHintContent]="hintContent"
[tuiHintDirection]="hintDirection"
[tuiHintAppearance]="hintAppearance"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
[tuiDropdownLimitWidth]="dropdownLimitWidth"
[tuiDropdownMinHeight]="dropdownMinHeight"
[tuiDropdownMaxHeight]="dropdownMaxHeight"
[tuiDropdownOffset]="dropdownOffset"
[tuiTextfieldCleaner]="cleaner"
[tuiTextfieldLabelOutside]="labelOutside"
[tuiTextfieldSize]="size"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
[tuiDropdownLimitWidth]="dropdownLimitWidth"
[tuiDropdownMinHeight]="dropdownMinHeight"
[tuiDropdownMaxHeight]="dropdownMaxHeight"
[tuiDropdownOffset]="dropdownOffset"
[tuiTextfieldCleaner]="cleaner"
[tuiTextfieldCustomContent]="customContent"
[tuiTextfieldLabelOutside]="labelOutside"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ <h3>To close dropdown:</h3>
[tuiDropdownAlign]="dropdownAlign"
[tuiDropdownDirection]="dropdownDirection"
[tuiDropdownLimitWidth]="dropdownLimitWidth"
[tuiDropdownOffset]="dropdownOffset"
>
Right click on me to
<strong>see a dropdown</strong>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
[tuiDropdownAlign]="dropdownAlign"
[tuiDropdownDirection]="dropdownDirection"
[tuiDropdownLimitWidth]="dropdownLimitWidth"
[tuiDropdownOffset]="dropdownOffset"
>
Hover pointer over
<strong>to see a dropdown</strong>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
[tuiDropdownAlign]="dropdownAlign"
[tuiDropdownDirection]="dropdownDirection"
[tuiDropdownLimitWidth]="dropdownLimitWidth"
[tuiDropdownOffset]="dropdownOffset"
>
Select a text to
<strong>see a dropdown</strong>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
[tuiDropdownDirection]="dropdownDirection"
[tuiDropdownLimitWidth]="dropdownLimitWidth"
[tuiDropdownManual]="open"
[tuiDropdownOffset]="dropdownOffset"
(tuiObscured)="onObscured($event)"
(click)="onClick()"
>
Expand Down

0 comments on commit d1ad281

Please sign in to comment.