@blueprintjs/core
Version:
Core styles & components
98 lines (87 loc) • 3.5 kB
text/typescript
/*
* Copyright 2015 Palantir Technologies, Inc. All rights reserved.
* Licensed under the BSD-3 License as modified (the “License”); you may obtain a copy
* of the license at https://github.com/palantir/blueprint/blob/master/LICENSE
* and https://github.com/palantir/blueprint/blob/master/PATENTS
*/
import * as Tether from "tether";
import { Position } from "./position";
const DEFAULT_CONSTRAINTS = {
attachment: "together",
to: "scrollParent",
};
export interface ITetherConstraint {
attachment?: string;
outOfBoundsClass?: string;
pin?: boolean | string[];
pinnedClass?: string;
to?: string | HTMLElement | number[];
}
/** @internal */
export function createTetherOptions(element: Element,
target: Node,
position: Position,
useSmartPositioning: boolean,
constraints: ITetherConstraint[]) {
if (constraints == null && useSmartPositioning) {
constraints = [DEFAULT_CONSTRAINTS];
}
const options: Tether.ITetherOptions = {
attachment: getPopoverAttachment(position),
classPrefix: "pt-tether",
constraints,
element,
target,
targetAttachment: getTargetAttachment(position),
};
return options;
}
/** @internal */
export function getTargetAttachment(position: Position) {
const attachments: {[p: number]: string} = {
[]: "top left",
[]: "top center",
[]: "top right",
[]: "top right",
[]: "middle right",
[]: "bottom right",
[]: "bottom right",
[]: "bottom center",
[]: "bottom left",
[]: "bottom left",
[]: "middle left",
[]: "top left",
};
return attachments[position];
}
/** @internal */
export function getPopoverAttachment(position: Position) {
const attachments: {[p: number]: string} = {
[]: "bottom left",
[]: "bottom center",
[]: "bottom right",
[]: "top left",
[]: "middle left",
[]: "bottom left",
[]: "top right",
[]: "top center",
[]: "top left",
[]: "bottom right",
[]: "middle right",
[]: "top right",
};
return attachments[position];
}
/** @internal */
export function getAttachmentClasses(position: Position) {
// this essentially reimplements the Tether logic for attachment classes so the same styles
// can be reused outside of Tether-based popovers.
return [
...expandAttachmentClasses(getPopoverAttachment(position), "pt-tether-element-attached"),
...expandAttachmentClasses(getTargetAttachment(position), "pt-tether-target-attached"),
];
}
function expandAttachmentClasses(attachments: string, prefix: string) {
const [verticalAlign, horizontalAlign] = attachments.split(" ");
return [`${prefix}-${verticalAlign}`, `${prefix}-${horizontalAlign}`];
}