Skip to content

feat: dynamically setting style through styles prop #846

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

Merged
merged 22 commits into from
Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
1ecf0e0
fix: tooltip arrow background color now inherit from tooltip backgrou…
danielbarion Dec 8, 2022
1da8465
docs: add gabrieljablonski as maintainer of the project
danielbarion Dec 8, 2022
8909abf
feat: add styles prop into tooltip
danielbarion Dec 8, 2022
7c48352
docs: update docs for styles attribute
danielbarion Dec 8, 2022
65dfd45
docs: add missed package into doc folder
danielbarion Dec 8, 2022
018e90d
docs: add contribuiton.md into the project
danielbarion Dec 8, 2022
1b16ff8
refactor: update grammarly of contribution file
danielbarion Dec 8, 2022
a814f4b
chore: increase feature version in package.json
danielbarion Dec 8, 2022
73d66b1
docs: update getting started docs
danielbarion Dec 9, 2022
0dc2d10
docs: update basic examples upgrade docs
danielbarion Dec 9, 2022
ab90baf
Update src/components/Tooltip/TooltipTypes.d.ts
danielbarion Dec 9, 2022
e302e36
Update src/components/TooltipController/TooltipControllerTypes.d.ts
danielbarion Dec 9, 2022
55d2b7f
chore: update comment on ts types build
danielbarion Dec 9, 2022
e44265d
refactor: use style instead styles and add docs for this in options
danielbarion Dec 9, 2022
69278fa
docs: add missed import into getting started docs
danielbarion Dec 9, 2022
28dd2cf
fix: update types build and dts package version to build decorators
danielbarion Dec 9, 2022
12333e7
chore: add codecov integration into the project
danielbarion Dec 9, 2022
a058d35
chore: remove codecov to do this in another PR
danielbarion Dec 9, 2022
972149b
docs: fix broken links pointed to v5 branch
danielbarion Dec 9, 2022
04a7abc
docs: fix h1 to improve SEO
danielbarion Dec 9, 2022
8d9b89e
docs: add description to some pages to improve docs SEO
danielbarion Dec 9, 2022
d9ef7d5
docs: add robots.txt into docs
danielbarion Dec 9, 2022
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
38 changes: 38 additions & 0 deletions CONTRIBUTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Contribution

## Running project locally

### ReactTooltip (root)

1. Run `npm install` || `yarn` to install dependencies
2. You can test by: `yarn dev` to run dev mode, or you can run **docs** directly using root folder instead of NPM, for this, please check **docs section**.

### Docs

You still need step 1 from `ReactTooltip (root)` section.
Docs are commited using npm packages, but with this steps, you can use local ReactTooltip package build.

1. In **docs** folder, run `npm install` || `yarn` to install dependencies
2. In **root** folder (parent of docs folder), run `yarn build` to generate a production build (you can use `yarn build --watch` to always generate a new build when a file changes)
3. Change `package.json`:

From this:
```
"react": "18.2.0",
"react-dom": "18.2.0",
"react-tooltip": "5.0.0"
```

To this:
```
"react": "link:../node_modules/react",
"react-dom": "link:../node_modules/react-dom",
"react-tooltip": "link:.."

```

4. Run `yarn start` and open `localhost:3000` to see docs running locally.

OBS: do not commit this change or the docs will broken the deployment and will not be updated.


2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ import ReactDOMServer from 'react-dom/server';

[danielbarion](https://github.com/danielbarion) Maintainer - Creator of React Tooltip >= V5.

[gabrieljablonski](https://github.com/gabrieljablonski) Maintainer.

[aronhelser](https://github.com/aronhelser) Passive maintainer - accepting PRs and doing minor testing, but not fixing issues or doing active development.

[alexgurr](https://github.com/alexgurr) (inactive).
Expand Down
3 changes: 2 additions & 1 deletion docs/docs/examples/_category_.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"label": "Examples",
"position": 4,
"link": {
"type": "generated-index"
"type": "generated-index",
"description": "Examples using props and some use cases with ReactTooltip component"
}
}
174 changes: 79 additions & 95 deletions docs/docs/examples/styling.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ sidebar_position: 1

How to customize tooltip styles in ReactTooltip styles.

Now tooltip arrow inherit his background color from tooltip (his parent).

import { Tooltip } from 'react-tooltip'
import 'react-tooltip/dist/react-tooltip.css'
import CodeBlock from '@theme/CodeBlock'
import TooltipStyles from '!!raw-loader!../../../src/components/Tooltip/styles.module.css'

export const TooltipAnchor = ({ children, id, ...rest }) => {
return (
Expand All @@ -33,31 +37,55 @@ export const TooltipAnchor = ({ children, id, ...rest }) => {
)
}

### Basic explanation
### Basic explanation - Inline Styling

You can add styles into the tooltip as inline styling.

```jsx
import { Tooltip } from 'react-tooltip'
import 'react-tooltip/dist/react-tooltip.css'

<a id="custom-inline-styles" data-tooltip-content="hello world!">
◕‿‿◕
</a>
<Tooltip
anchorId="custom-inline-styles"
style={{ backgroundColor: "rgb(0, 255, 30)", color: "#222" }}
/>
```

<div
style={{ display: 'flex', columnGap: '16px', justifyContent: 'center' }}
>
<TooltipAnchor id="custom-inline-styles" data-tooltip-content="hello world!">
◕‿‿◕
</TooltipAnchor>
<Tooltip anchorId="custom-inline-styles" style={{ backgroundColor: "rgb(0, 255, 30)", color: "#222" }} />
</div>


### Basic explanation - Classes

You don't need `!important` anymore, you just need to know at least a bit about CSS Specificity ([MDN Docs](https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity) | [W3c Docs](https://www.w3schools.com/css/css_specificity.asp)).
Using CSS Specificity you can add a class with more specificity than the current used in tooltip and you can override or add new rules to the component style.


```jsx
import { Tooltip } from 'react-tooltip'
import 'react-tooltip/dist/react-tooltip.css'

<style>
.example-container .example {
.example {
color: #222;
background-color: rgb(0, 247, 255);
}

.example-container .example .example-arrow {
background-color: rgb(0, 247, 255);
}
</style>

<div className="example-container">
<a id="custom-styles" data-tooltip-content="hello world!">
◕‿‿◕
</a>
<Tooltip anchorId="custom-styles" className="example" classNameArrow="example-arrow" />
<Tooltip anchorId="custom-styles" className="example" />
</div>
```

Expand All @@ -68,74 +96,14 @@ import 'react-tooltip/dist/react-tooltip.css'
<TooltipAnchor id="custom-styles" data-tooltip-content="hello world!">
◕‿‿◕
</TooltipAnchor>
<Tooltip anchorId="custom-styles" className="example" classNameArrow="example-arrow" />
<Tooltip anchorId="custom-styles" className="example" />
</div>

#### Explanation

In this example, we are adding an extra level to the CSS classes, the following styles are the default one of Tooltip component when we write this docs:

```css
.tooltip {
visibility: hidden;
width: max-content;
position: absolute;
top: 0;
left: 0;
padding: 8px 16px;
border-radius: 3px;
font-size: 90%;
pointer-events: none;
opacity: 0;
transition: opacity 0.3s ease-out;
will-change: opacity, visibility;
}

.arrow {
position: absolute;
background: var(--rt-color-dark);
width: 8px;
height: 8px;
transform: rotate(45deg);
}

/** Types variant **/
.dark,
.dark .arrow {
background: var(--rt-color-dark);
color: var(--rt-color-white);
}

.light,
.light .arrow {
background-color: var(--rt-color-white);
color: var(--rt-color-dark);
}

.success,
.success .arrow {
background-color: var(--rt-color-success);
color: var(--rt-color-white);
}

.warning,
.warning .arrow {
background-color: var(--rt-color-warning);
color: var(--rt-color-white);
}

.error,
.error .arrow {
background-color: var(--rt-color-error);
color: var(--rt-color-white);
}

.info,
.info .arrow {
background-color: var(--rt-color-info);
color: var(--rt-color-white);
}
```
<CodeBlock language="css">{TooltipStyles}</CodeBlock>

So, if we only add new classes like the below, this will not work because this is the same level of specificity than the default dark variant as example, you can compare:

Expand All @@ -145,20 +113,9 @@ So, if we only add new classes like the below, this will not work because this i
background-color: rgb(0, 247, 255);
}

/** add next line only if you want to have tooltip arrow with a different background color from tooltip **/
.example .example-arrow {
background-color: rgb(0, 247, 255);
}
```

```css
.tooltip {
...;
}

.dark,
.dark .arrow {
background: var(--rt-color-dark);
color: var(--rt-color-white);
background-color: rgb(255, 0, 0);
}
```

Expand All @@ -170,8 +127,9 @@ So, to make this work as expected, we need to add a new more level like this one
background-color: rgb(0, 247, 255);
}

/** add next line only if you want to have tooltip arrow with a different background color from tooltip **/
.some-class-or-rule .example .example-arrow {
background-color: rgb(0, 247, 255);
background-color: rgb(255, 0, 0);
}
```

Expand All @@ -192,17 +150,13 @@ import 'react-tooltip/dist/react-tooltip.css'
color: #222;
background-color: rgb(255, 153, 0);
}

.example-container .example-orange .example-arrow {
background-color: rgb(255, 153, 0);
}
</style>

<div className="example-container">
<a id="custom-styles-orange" data-tooltip-content="hello world!">
◕‿‿◕
</a>
<Tooltip anchorId="custom-styles-orange" className="example-orange" classNameArrow="example-arrow" />
<Tooltip anchorId="custom-styles-orange" className="example-orange" />
</div>
```

Expand All @@ -216,7 +170,6 @@ import 'react-tooltip/dist/react-tooltip.css'
<Tooltip
anchorId="custom-styles-orange"
className="example-orange"
classNameArrow="example-arrow"
/>
</div>

Expand All @@ -231,17 +184,13 @@ import 'react-tooltip/dist/react-tooltip.css'
color: #fff;
background-color: rgb(255, 0, 255);
}

.example-container .example-pink .example-arrow {
background-color: rgb(255, 0, 255);
}
</style>

<div className="example-container">
<a id="custom-styles-pink" data-tooltip-content="hello world!">
◕‿‿◕
</a>
<Tooltip anchorId="custom-styles-pink" className="example-pink" classNameArrow="example-arrow" />
<Tooltip anchorId="custom-styles-pink" className="example-pink" />
</div>
```

Expand All @@ -252,7 +201,42 @@ import 'react-tooltip/dist/react-tooltip.css'
<TooltipAnchor id="custom-styles-pink" data-tooltip-content="hello world!">
◕‿‿◕
</TooltipAnchor>
<Tooltip anchorId="custom-styles-pink" className="example-pink" classNameArrow="example-arrow" />
<Tooltip anchorId="custom-styles-pink" className="example-pink" />
</div>

#### Tooltip Arrow with a different color from Tooltip

```jsx
import { Tooltip } from 'react-tooltip'
import 'react-tooltip/dist/react-tooltip.css'

<style>
.example-container .example-diff-arrow {
color: #fff;
background-color: rgb(55, 55, 55);
}

.example-container .example-diff-arrow .example-arrow {
background-color: rgb(222, 34, 72);
}
</style>

<div className="example-container">
<a id="custom-styles-diff" data-tooltip-content="hello world!">
◕‿‿◕
</a>
<Tooltip anchorId="custom-styles-diff" className="example-diff-arrow" classNameArrow="example-arrow" />
</div>
```

<div
className="example-container"
style={{ display: 'flex', columnGap: '16px', justifyContent: 'center' }}
>
<TooltipAnchor id="custom-styles-diff" data-tooltip-content="hello world!">
◕‿‿◕
</TooltipAnchor>
<Tooltip anchorId="custom-styles-diff" className="example-diff-arrow" classNameArrow="example-arrow" />
</div>

### More examples - Radius
Expand Down
6 changes: 5 additions & 1 deletion docs/docs/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,15 @@ import ReactTooltip from 'react-tooltip'
2 . Add data-content = "your placeholder" to your element

```jsx
<p id="my-anchor-element" data-content="hello world" data-place="top">
<p id="my-anchor-element" data-tooltip-content="hello world" data-tooltip-place="top">
Tooltip
</p>
```

### Styling

If you want a styled tooltip, don't forget to add the style file `import 'react-tooltip/dist/react-tooltip.css`

3 . Include react-tooltip component

```jsx
Expand Down
1 change: 1 addition & 0 deletions docs/docs/options.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import 'react-tooltip/dist/react-tooltip.css'
| positionStrategy | string | false | `absolute` | `absolute` `fixed` | the position strategy used for the tooltip. set to `fixed` if you run into issues with `overflow: hidden` on the tooltip parent container |
| delayShow | number | false | | any `number` | tooltip show will be delayed in miliseconds by the amount of value |
| delayHide | number | false | | any `number` | tooltip hide will be delayed in miliseconds by the amount of value |
| style | CSSProperties | false | | any React inline style | add styles directly to the component by `style` attribute |
| getContent | function | false | | (value: string) => string | a method available to parse, format or modify the given content of tooltip before show it |
| isOpen | boolen | false | handled by internal state | `true` `false` | the tooltip can be controlled or uncontrolled, this attribute can be used to handle show and hide tooltip outside tooltip (can be used **without** `setIsOpen`) |
| setIsOpen | function | false | | | the tooltip can be controlled or uncontrolled, this attribute can be used to handle show and hide tooltip outside tooltip |
Expand Down
3 changes: 2 additions & 1 deletion docs/docs/upgrade-guide/_category_.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"label": "Upgrade Guide",
"position": 2,
"link": {
"type": "generated-index"
"type": "generated-index",
"description": "Upgrade docs related to migration from V4 to V5"
}
}
4 changes: 3 additions & 1 deletion docs/docs/upgrade-guide/basic-examples-v4-v5.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ sidebar_position: 4

# Basic examples V4 -> V5

Examples of use in V4 -> V5

import { Tooltip } from 'react-tooltip'
import 'react-tooltip/dist/react-tooltip.css'

Expand Down Expand Up @@ -78,7 +80,7 @@ import 'react-tooltip/dist/react-tooltip.css';

---

# Colors
## Colors

### V4 or less - type

Expand Down
Loading