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 1, 2022
1 parent 1d78810 commit 710651c
Show file tree
Hide file tree
Showing 21 changed files with 59 additions and 37 deletions.
19 changes: 13 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,14 @@ 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 verticalOffset: number;
readonly horizontalOffset: number;
}

/** Default values for dropdown options */
Expand All @@ -36,6 +33,8 @@ export const TUI_DROPDOWN_DEFAULT_OPTIONS: TuiDropdownOptions = {
limitWidth: `auto`,
maxHeight: 400,
minHeight: 80,
verticalOffset: 4,
horizontalOffset: 4,
};

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

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

@Input(`tuiDropdownVerticalOffset`)
@tuiDefaultProp()
verticalOffset = this.options.verticalOffset;

@Input(`tuiDropdownHorizontalOffset`)
@tuiDefaultProp()
horizontalOffset = this.options.horizontalOffset;

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.horizontalOffset,
right: innerWidth - hostRect.right - this.options.horizontalOffset,
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 + 2 * this.options.verticalOffset + 1, // 1 for border
left: hostRect.left - width - this.options.horizontalOffset,
right: hostRect.right + this.options.horizontalOffset,
bottom: hostRect.top - 2 * this.options.verticalOffset - 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.horizontalOffset);
const available = {
top: hostRect.top - 2 * TUI_DROPDOWN_OFFSET,
bottom: innerHeight - hostRect.bottom - 2 * TUI_DROPDOWN_OFFSET,
top: hostRect.top - 2 * this.options.verticalOffset,
bottom: innerHeight - hostRect.bottom - 2 * this.options.verticalOffset,
} as const;
const position = {
top: hostRect.top - TUI_DROPDOWN_OFFSET - height,
bottom: hostRect.bottom + TUI_DROPDOWN_OFFSET,
top: hostRect.top - this.options.verticalOffset - height,
bottom: hostRect.bottom + this.options.verticalOffset,
right,
left:
hostRect.left + width < innerWidth - TUI_DROPDOWN_OFFSET
hostRect.left + width < innerWidth - this.options.horizontalOffset
? 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 @@ -27,11 +27,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 @@ -104,13 +100,13 @@ export class TuiDropdownComponent {
const isIntersecting =
left < rect.right &&
right > rect.left &&
top < offsetY + 2 * TUI_DROPDOWN_OFFSET;
top < offsetY + 2 * this.options.verticalOffset;
const available = isIntersecting
? rect.top - 2 * TUI_DROPDOWN_OFFSET
: offsetY + innerHeight - top - TUI_DROPDOWN_OFFSET;
? rect.top - 2 * this.options.verticalOffset
: offsetY + innerHeight - top - this.options.verticalOffset;

style.position = position;
style.top = tuiPx(Math.max(top, offsetY + TUI_DROPDOWN_OFFSET));
style.top = tuiPx(Math.max(top, offsetY + this.options.verticalOffset));
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,8 @@ const DROPDOWN_CONTROLLER_SELECTORS = [
`tuiDropdownLimitWidth`,
`tuiDropdownMinHeight`,
`tuiDropdownMaxHeight`,
`tuiDropdownHorizontalOffset`,
`tuiDropdownVerticalOffset`,
];

@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 @@ -139,6 +139,8 @@ export abstract class AbstractExampleTuiControl

dropdownMaxHeight = TUI_DROPDOWN_DEFAULT_OPTIONS.maxHeight;

dropdownVerticalOffset = TUI_DROPDOWN_DEFAULT_OPTIONS.verticalOffset;

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="dropdownVerticalOffset"
documentationPropertyMode="input"
documentationPropertyType="number"
[(documentationPropertyValue)]="documentedComponent.dropdownVerticalOffset"
>
Dropdown vertical 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;
dropdownVerticalOffset = TUI_DROPDOWN_DEFAULT_OPTIONS.verticalOffset;
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
[tuiDropdownLimitWidth]="dropdownLimitWidth"
[tuiDropdownMinHeight]="dropdownMinHeight"
[tuiDropdownMaxHeight]="dropdownMaxHeight"
[tuiDropdownVerticalOffset]="dropdownVerticalOffset"
[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"
[tuiDropdownVerticalOffset]="dropdownVerticalOffset"
[(open)]="open"
>
<tui-input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
[tuiDropdownDirection]="dropdownDirection"
[tuiDropdownMinHeight]="dropdownMinHeight"
[tuiDropdownMaxHeight]="dropdownMaxHeight"
[tuiDropdownVerticalOffset]="dropdownVerticalOffset"
[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"
[tuiDropdownVerticalOffset]="dropdownVerticalOffset"
[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"
[tuiDropdownVerticalOffset]="dropdownVerticalOffset"
[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"
[tuiDropdownVerticalOffset]="dropdownVerticalOffset"
[items]="items"
[itemSize]="itemSize"
[tuiTextfieldCleaner]="cleaner"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@
[tuiDropdownLimitWidth]="dropdownLimitWidth"
[tuiDropdownMinHeight]="dropdownMinHeight"
[tuiDropdownMaxHeight]="dropdownMaxHeight"
[tuiDropdownVerticalOffset]="dropdownVerticalOffset"
[tuiHintContent]="hintContent"
[tuiHintDirection]="hintDirection"
[tuiHintAppearance]="hintAppearance"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
[tuiDropdownLimitWidth]="dropdownLimitWidth"
[tuiDropdownMinHeight]="dropdownMinHeight"
[tuiDropdownMaxHeight]="dropdownMaxHeight"
[tuiDropdownVerticalOffset]="dropdownVerticalOffset"
[tuiTextfieldCleaner]="cleaner"
[tuiTextfieldLabelOutside]="labelOutside"
[tuiTextfieldSize]="size"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
[tuiDropdownLimitWidth]="dropdownLimitWidth"
[tuiDropdownMinHeight]="dropdownMinHeight"
[tuiDropdownMaxHeight]="dropdownMaxHeight"
[tuiDropdownVerticalOffset]="dropdownVerticalOffset"
[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"
[tuiDropdownVerticalOffset]="dropdownVerticalOffset"
>
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"
[tuiDropdownVerticalOffset]="dropdownVerticalOffset"
>
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"
[tuiDropdownVerticalOffset]="dropdownVerticalOffset"
>
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"
[tuiDropdownVerticalOffset]="dropdownVerticalOffset"
(tuiObscured)="onObscured($event)"
(click)="onClick()"
>
Expand Down

0 comments on commit 710651c

Please sign in to comment.