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

Remove tooltip default styles #666

Merged
merged 6 commits into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions packages/vx-tooltip/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ This is a simple Tooltip container component meant to be used to actually render
| className | string | -- | Adds a class (in addition to `vx-tooltip-portal`) to the tooltip container |
| style | object | -- | Sets / overrides any styles on the tooltip container (including top and left) |
| children | node | -- | Sets the children of the tooltip, i.e., the actual content |
| unstyled | bool | true | Whether the tooltip use styles from the style prop or not |

#### TooltipWithBounds

Expand Down
2 changes: 1 addition & 1 deletion packages/vx-tooltip/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"dependencies": {
"@types/classnames": "^2.2.9",
"@types/react": "*",
"@vx/bounds": "0.0.195",
"@vx/bounds": "^0.0.195",
williaster marked this conversation as resolved.
Show resolved Hide resolved
"classnames": "^2.2.5",
"prop-types": "^15.5.10"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/vx-tooltip/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { default as withTooltip } from './enhancers/withTooltip';
export { default as useTooltip } from './hooks/useTooltip';
export { default as Tooltip } from './tooltips/Tooltip';
export { default as Tooltip, defaultStyles } from './tooltips/Tooltip';
export { default as TooltipWithBounds } from './tooltips/TooltipWithBounds';
31 changes: 16 additions & 15 deletions packages/vx-tooltip/src/tooltips/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,34 @@ export type TooltipProps = {
className?: string;
style?: React.CSSProperties;
children?: React.ReactNode;
unstyled?: boolean;
};

export const defaultStyles: React.CSSProperties = {
position: 'absolute',
backgroundColor: 'white',
color: '#666666',
padding: '.3rem .5rem',
borderRadius: '3px',
fontSize: '14px',
boxShadow: '0 1px 2px rgba(33,33,33,0.2)',
lineHeight: '1em',
pointerEvents: 'none',
};

export default function Tooltip({
className,
top,
left,
style,
style = defaultStyles,
children,
unstyled = false,
...restProps
}: TooltipProps & JSX.IntrinsicElements['div']) {
return (
<div
className={cx('vx-tooltip-portal', className)}
style={{
position: 'absolute',
backgroundColor: 'white',
color: '#666666',
padding: '.3rem .5rem',
borderRadius: '3px',
fontSize: '14px',
boxShadow: '0 1px 2px rgba(33,33,33,0.2)',
lineHeight: '1em',
pointerEvents: 'none',
top,
left,
...style,
}}
style={{ top, left, ...(!unstyled && style) }}
{...restProps}
>
{children}
Expand Down
1 change: 0 additions & 1 deletion packages/vx-tooltip/src/tooltips/TooltipWithBounds.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';
// @ts-ignore This line could be removed after bounds migration to the TS
import { withBoundingRects } from '@vx/bounds';

import Tooltip from './Tooltip';
Expand Down
38 changes: 37 additions & 1 deletion packages/vx-tooltip/test/Tooltip.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,43 @@
import { Tooltip } from '../src';
import React from 'react';
import { shallow } from 'enzyme';
import { Tooltip, defaultStyles } from '../src';

describe('<Tooltip />', () => {
test('it should be defined', () => {
expect(Tooltip).toBeDefined();
});

it('should render with the default styles', () => {
const wrapper = shallow(<Tooltip>Hello</Tooltip>);
const styles = wrapper.props().style;
Object.entries(defaultStyles).forEach(([key, value]) => {
expect(styles[key]).toBe(value);
});
});

it('should render with no default styles', () => {
const wrapper = shallow(<Tooltip unstyled>Hello</Tooltip>);
const styles = wrapper.props().style;
Object.keys(defaultStyles).forEach(key => {
expect(styles[key]).toBe(undefined);
});
});

it('should overwrite default styles when given the style prop', () => {
const newStyles: React.CSSProperties = {
position: 'relative',
backgroundColor: 'green',
color: 'red',
padding: '.8rem .8rem',
borderRadius: '13px',
fontSize: '17px',
boxShadow: '0 2px 3px rgba(133,133,133,0.5)',
lineHeight: '2em',
};
const wrapper = shallow(<Tooltip style={newStyles}></Tooltip>);
const styles = wrapper.props().style;
Object.entries(newStyles).forEach(([key, value]) => {
expect(styles[key]).toBe(value);
});
});
});