Skip to content

Enable arrow rounding #110

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
63 changes: 40 additions & 23 deletions src/components/Floater/Arrow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,54 @@ interface Props {
styles: Styles;
}

/**
* FloaterArrow component renders a customizable arrow for tooltips/popovers
* @param props Component properties
* @returns React component
*/
export default function FloaterArrow(props: Props) {
const { arrowRef, placement, styles } = props;

const {
arrow: { color, display, length, position, spread },
arrow: { color, display, length, position, rounded, spread },
} = styles;

const [direction] = placement.split('-');
const isVertical = direction === 'top' || direction === 'bottom';

const arrowStyles: React.CSSProperties = { display, position };

let points;
let x = spread;
let y = length;

if (placement.startsWith('top')) {
points = `0,0 ${x / 2},${y} ${x},0`;
} else if (placement.startsWith('bottom')) {
points = `${x},${y} ${x / 2},0 0,${y}`;
} else if (placement.startsWith('left')) {
y = spread;
x = length;
points = `0,0 ${x},${y / 2} 0,${y}`;
} else if (placement.startsWith('right')) {
y = spread;
x = length;
points = `${x},${y} ${x},0 0,${y / 2}`;
const baseRadius = isVertical ? spread / 8 : length / 8;
const r = rounded ? baseRadius : 0;

const webkitMask = isVertical
? `linear-gradient(0deg,#0000 calc(${r}px/sqrt(2)),#000 0),
radial-gradient(${r}px at 50% calc(100% - ${r}px*sqrt(2)),#000 98%,#0000 101%)`
: `linear-gradient(-90deg,#0000 calc(${r}px/sqrt(2)),#000 0),
radial-gradient(${r}px at calc(100% - ${r}px*sqrt(2)) 50%,#000 98%,#0000 101%)`;

const clipPath = isVertical ? 'polygon(50% 100%,100% 0,0 0)' : 'polygon(100% 50%,0 100%,0 0)';

let scales: string | undefined;

if (direction === 'bottom') {
scales = '1 -1';
} else if (direction === 'right') {
scales = '-1 1';
}

return (
<span ref={arrowRef} className="__floater__arrow" style={arrowStyles}>
<svg height={y} version="1.1" width={x} xmlns="http://www.w3.org/2000/svg">
<polygon fill={color} points={points} />
</svg>
</span>
<span
ref={arrowRef}
className="__floater__arrow"
style={{
...arrowStyles,
backgroundColor: color,
height: `${isVertical ? length : spread}px`,
width: `${isVertical ? spread : length}px`,
WebkitMask: rounded ? webkitMask : 'none',
clipPath,
scale: scales,
}}
/>
);
}
1 change: 1 addition & 0 deletions src/modules/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default function getStyles(styles?: PartialDeep<Styles>): Styles {
length: 16,
position: 'absolute',
spread: 32,
rounded: false,
},
close: {
backgroundColor: 'transparent',
Expand Down
31 changes: 16 additions & 15 deletions src/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,19 @@ import {
} from 'react';
import { PartialDeep, RequireExactlyOne, ValueOf } from 'type-fest';

import { PopperInstance, PopperModifiers, PopperPlacement } from './popper';

import { STATUS } from '../literals';

import { PopperInstance, PopperModifiers, PopperPlacement } from './popper';

export type Action = 'open' | 'close';
export type CloseFunction<T = HTMLElement> = MouseEventHandler<T>;
export type FloaterComponent<T = CustomComponentProps> = FunctionComponent<T> | ReactElement<T>;
export type Placement = PopperPlacement | 'center';
export type SelectorOrElement = string | null | HTMLElement;
export type Statuses = ValueOf<typeof STATUS>;

export interface CustomComponentProps {
closeFn: CloseFunction;
}
export type Props = RequireExactlyOne<BaseProps, 'content' | 'component'>;

export interface LogOptions {
data: any;
debug?: boolean;
title: string;
}
export type SelectorOrElement = string | null | HTMLElement;

export type FloaterComponent<T = CustomComponentProps> = FunctionComponent<T> | ReactElement<T>;
export type Statuses = ValueOf<typeof STATUS>;

export interface BaseProps {
/**
Expand Down Expand Up @@ -121,7 +113,15 @@ export interface BaseProps {
};
}

export type Props = RequireExactlyOne<BaseProps, 'content' | 'component'>;
export interface CustomComponentProps {
closeFn: CloseFunction;
}

export interface LogOptions {
data: any;
debug?: boolean;
title: string;
}

export interface State {
currentPlacement: Placement;
Expand All @@ -133,6 +133,7 @@ export interface State {
export interface Styles {
arrow: CSSProperties & {
length: number;
rounded: boolean;
spread: number;
};
close: CSSProperties;
Expand Down
96 changes: 12 additions & 84 deletions test/__snapshots__/index.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,8 @@ exports[`ReactFloater > with \`component\` as element > should show the floater
</div>
<span
class="__floater__arrow"
style="display: inline-flex; position: absolute; left: 0px; transform: translate(8px, 0px); top: 0px;"
>
<svg
height="16"
version="1.1"
width="32"
xmlns="http://www.w3.org/2000/svg"
>
<polygon
fill="#fff"
points="32,16 16,0 0,16"
/>
</svg>
</span>
style="display: inline-flex; position: absolute; background-color: rgb(255, 255, 255); height: 16px; width: 32px; clip-path: polygon(50% 100%,100% 0,0 0); scale: 1 -1; left: 0px; transform: translate(8px, 0px); top: 0px;"
/>
</div>
</div>
`;
Expand Down Expand Up @@ -66,20 +54,8 @@ exports[`ReactFloater > with \`component\` as function > should show the floater
</div>
<span
class="__floater__arrow"
style="display: inline-flex; position: absolute; left: 0px; transform: translate(8px, 0px); top: 0px;"
>
<svg
height="16"
version="1.1"
width="32"
xmlns="http://www.w3.org/2000/svg"
>
<polygon
fill="#fff"
points="32,16 16,0 0,16"
/>
</svg>
</span>
style="display: inline-flex; position: absolute; background-color: rgb(255, 255, 255); height: 16px; width: 32px; clip-path: polygon(50% 100%,100% 0,0 0); scale: 1 -1; left: 0px; transform: translate(8px, 0px); top: 0px;"
/>
</div>
</div>
`;
Expand Down Expand Up @@ -132,20 +108,8 @@ exports[`ReactFloater > with \`placement\` left > should show the floater with c
</div>
<span
class="__floater__arrow"
style="display: inline-flex; position: absolute; top: 0px; transform: translate(0px, 8px); right: 0px;"
>
<svg
height="32"
version="1.1"
width="16"
xmlns="http://www.w3.org/2000/svg"
>
<polygon
fill="#fff"
points="0,0 16,16 0,32"
/>
</svg>
</span>
style="display: inline-flex; position: absolute; background-color: rgb(255, 255, 255); height: 32px; width: 16px; clip-path: polygon(100% 50%,0 100%,0 0); top: 0px; transform: translate(0px, 8px); right: 0px;"
/>
</div>
</div>
`;
Expand Down Expand Up @@ -173,20 +137,8 @@ exports[`ReactFloater > with \`placement\` right > should show the floater with
</div>
<span
class="__floater__arrow"
style="display: inline-flex; position: absolute; top: 0px; transform: translate(0px, 8px); left: 0px;"
>
<svg
height="32"
version="1.1"
width="16"
xmlns="http://www.w3.org/2000/svg"
>
<polygon
fill="#fff"
points="16,32 16,0 0,16"
/>
</svg>
</span>
style="display: inline-flex; position: absolute; background-color: rgb(255, 255, 255); height: 32px; width: 16px; clip-path: polygon(100% 50%,0 100%,0 0); scale: -1 1; top: 0px; transform: translate(0px, 8px); left: 0px;"
/>
</div>
</div>
`;
Expand Down Expand Up @@ -214,20 +166,8 @@ exports[`ReactFloater > with \`placement\` top > should show the floater with cl
</div>
<span
class="__floater__arrow"
style="display: inline-flex; position: absolute; left: 0px; transform: translate(8px, 0px); bottom: 0px;"
>
<svg
height="16"
version="1.1"
width="32"
xmlns="http://www.w3.org/2000/svg"
>
<polygon
fill="#fff"
points="0,0 16,16 32,0"
/>
</svg>
</span>
style="display: inline-flex; position: absolute; background-color: rgb(255, 255, 255); height: 16px; width: 32px; clip-path: polygon(50% 100%,100% 0,0 0); left: 0px; transform: translate(8px, 0px); bottom: 0px;"
/>
</div>
</div>
`;
Expand Down Expand Up @@ -288,20 +228,8 @@ exports[`ReactFloater > with \`title\`, \`footer\` and \`closeBtn\` > should ren
</div>
<span
class="__floater__arrow"
style="display: inline-flex; position: absolute; left: 0px; transform: translate(8px, 0px); top: 0px;"
>
<svg
height="16"
version="1.1"
width="32"
xmlns="http://www.w3.org/2000/svg"
>
<polygon
fill="#fff"
points="32,16 16,0 0,16"
/>
</svg>
</span>
style="display: inline-flex; position: absolute; background-color: rgb(255, 255, 255); height: 16px; width: 32px; clip-path: polygon(50% 100%,100% 0,0 0); scale: 1 -1; left: 0px; transform: translate(8px, 0px); top: 0px;"
/>
</div>
</div>
`;