Skip to content

Commit

Permalink
fix: Allow icon to be customised and support hover over custom dom nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
rcdexta committed May 12, 2018
1 parent ec34a1a commit 6cf97cc
Show file tree
Hide file tree
Showing 14 changed files with 523 additions and 135 deletions.
2 changes: 2 additions & 0 deletions .storybook/addons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import '@storybook/addon-options/register'
import '@storybook/addon-storysource/register'
16 changes: 12 additions & 4 deletions .storybook/config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { configure } from '@storybook/react';
import {configure} from '@storybook/react'
import {setOptions} from '@storybook/addon-options'

setOptions({
name: 'React ToggleTip',
url: 'https://github.com/rcdexta/react-toggletip'
})


// automatically import all files ending in *.stories.js
const req = require.context('../stories', true, /.stories.js$/);
const req = require.context('../stories', true, /.stories.js$/)
function loadStories() {
req.keys().forEach(filename => req(filename));
req.keys().forEach(filename => req(filename))
}

configure(loadStories, module);

configure(loadStories, module)
6 changes: 5 additions & 1 deletion .storybook/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ module.exports = {
],
module: {
rules: [
// add your custom rules.
{
test: /\.stories\.js?$/,
loaders: [require.resolve('@storybook/addon-storysource/loader')],
enforce: 'pre',
},
],
},
};
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ Simple toggletip and tooltip for your react apps

![toggletip](https://github.com/rcdexta/react-toggletip/raw/master/assets/screenshot.png)

### Why?

I wanted a simple light-weight tooltip library for react that just does one good thing and does it well - displaying tooltips on hover

For context as to why it's called a "toggletip", read this [blog](https://inclusive-components.design/tooltips-toggletips/) for details

### Features

* works on all mainstream browsers
* css only tooltip without additional javascript dependencies
* uses `aria` attributes for accessibility and to work with screen readers
* works on icons and other elements
* supports multiline tooltips

### Installation

```bash
Expand All @@ -23,6 +37,20 @@ $ yarn add react-toggletip

https://rcdexta.github.io/react-toggletip

### Documentation

ToggleTip component will render a info icon by default. The default icon can be overridden with props given below. If you want to display tooltips over custom content, wrap ToggleTip around the content.

| Name | Type | Description |
| --------- | -------------------------- | ------------------------------------------------------------ |
| content | String or array of strings | The tooltip content. Pass array of strings if you want multiline tooltips |
| placement | string | one of `bottom-right`, `bottom`, `bottom-left`,`right`,`left`,`top-right`,`top`,`top-left` |
| animated | boolean | Disable tooltip animations. Default: true |
| icon | node | Replace info icon with custom icon |
| children | node | Display tooltip on top of custom content instead of icons |


### License

MIT

Binary file modified assets/screenshot.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 8 additions & 3 deletions components/ToggleTip.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ class ToggleTip extends React.Component {
return classes.join(' ')
}

get content() {
return this.props.children || this.props.icon || <InfoIcon style={{cursor: 'pointer', verticalAlign: 'none'}} />
}

render () {
return (
<span className={this.ttlpClasses} style={{display: 'flex', marginLeft: 3}} aria-label={this.label}>
<InfoIcon style={{cursor: 'pointer', verticalAlign: 'none'}} />
<span className={this.ttlpClasses} style={{display: 'flex', marginLeft: 3}} aria-label={this.label}>
{this.content}
</span>
)
}
Expand All @@ -42,7 +46,8 @@ ToggleTip.propTypes = {
'top',
'top-left'
]),
animated: PropTypes.bool
animated: PropTypes.bool,
icon: PropTypes.node
}

ToggleTip.defaultProps = {
Expand Down
18 changes: 11 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
"module": "components/index.js",
"files": [
"src",
"dist",
"README"
],
"scripts": {
"lint": "eslint components/** tests/**",
"lintfix": "eslint --fix components/** tests/**",
"prepublish": "npm run lint && npm run build",
"prepublish": "npm run lint",
"semantic-release": "semantic-release",
"storybook": "start-storybook -p 6006",
"test": "mocha --require tests/config/setup 'tests/**/*.test.js'",
Expand All @@ -39,10 +38,9 @@
"license": "MIT",
"homepage": "https://github.com/rcdexta/react-toggletip",
"devDependencies": {
"@storybook/addon-actions": "^3.4.3",
"@storybook/addon-knobs": "^3.4.3",
"@storybook/addon-links": "^3.4.3",
"@storybook/addon-options": "^3.4.3",
"@storybook/addon-storysource": "^3.4.3",
"@storybook/addons": "^3.4.3",
"@storybook/react": "^3.4.3",
"autoprefixer": "^8.4.1",
Expand Down Expand Up @@ -73,21 +71,27 @@
"jsdom": "^11.10.0",
"mocha": "^5.1.1",
"node-sass": "^4.9.0",
"polished": "^1.9.2",
"prop-types": "^15.6.1",
"react": "^16.3.2",
"react-addons-test-utils": "^15.6.2",
"react-dom": "^16.3.2",
"react-live": "^1.10.1",
"rimraf": "^2.6.2",
"sass-loader": "^7.0.1",
"semantic-release": "^15.1.8",
"sinon": "^5.0.1",
"webpack": "^4.6.0",
"travis-deploy-once": "^5.0.0"
"styled-components": "^3.2.6",
"travis-deploy-once": "^5.0.0",
"webpack": "^4.6.0"
},
"dependencies": {
"react-icons": "2.2.7"
},
"peerDependencies": {
"prop-types": ">= 15.5.7 < 17.0.0.-0",
"react": ">= 0.14.0 < 17.0.0-0",
"react-dom": ">= 0.14.0 < 17.0.0-0",
"prop-types": ">= 15.5.7 < 17.0.0.-0",
"styled-components": ">= 1.0.0 < 4.0.0"
},
"config": {
Expand Down
20 changes: 20 additions & 0 deletions stories/basic.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {storiesOf} from '@storybook/react'
import React from 'react'
import ToggleTip from '../components/ToggleTip'

storiesOf('ToggleTip', module).add('Basic', () => {
return (
<div className="elements">
<div className="btn-group">
<div className="btn two">
<span>Net Price</span>
<ToggleTip content="Price of item before tax" />
</div>
<div className="btn two">
<span>Gross Price</span>
<ToggleTip content="Price of item including tax and service charges" />
</div>
</div>
</div>
)
})
36 changes: 36 additions & 0 deletions stories/custom.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {storiesOf} from '@storybook/react'
import React from 'react'
import ToggleTip from '../components/ToggleTip'
import QuestionIcon from 'react-icons/lib/fa/question-circle'

storiesOf('ToggleTip', module).add('Custom Icon', () => {
return (
<div className="elements">
<div className="btn-group">
<div className="btn two">
<span>Unit Economics</span>
<ToggleTip
content="Unit economics are the direct revenues and costs associated with a particular business model expressed on a per unit basis."
icon={<QuestionIcon />}
/>
</div>
</div>
</div>
)
})


storiesOf('ToggleTip', module).add('Multiline', () => {
return (
<div className="elements">
<div className="btn-group">
<div className="btn two">
<span>Taxable Income: $850</span>
<ToggleTip
content={['Federal Tax: $550', 'Social Security: $300']}
/>
</div>
</div>
</div>
)
})
25 changes: 25 additions & 0 deletions stories/helpers/LiveEdit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react'
import styled from 'styled-components'
import * as polished from 'polished'

import { LiveProvider, LiveEditor} from 'react-live'

const StyledProvider = styled(LiveProvider)`
border-radius: ${polished.rem(3)};
box-shadow: 1px 1px 20px rgba(20, 20, 20, 0.27);
overflow: hidden;
`

const StyledEditor = styled(LiveEditor)`
font-size: 0.8rem;
cursor: text;
font-family: 'Source Code Pro', Monaco, Monospace,serif;
`

const LiveEdit = ({ noInline, code, scope }) => (
<StyledProvider code={code} scope={scope}>
<StyledEditor />
</StyledProvider>
)

export default LiveEdit
51 changes: 5 additions & 46 deletions stories/index.stories.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,11 @@
import React from 'react'

import {storiesOf, addDecorator} from '@storybook/react'
import {linkTo} from '@storybook/addon-links'
import {Welcome} from '@storybook/react/demo'
import ToggleTip from '../components/ToggleTip'
import {addDecorator} from '@storybook/react'

addDecorator(story => <div style={{margin: 30}}>{story()}</div>)

storiesOf('Welcome', module).add('to Storybook', () => <Welcome showApp={linkTo('Button')} />)
import './basic.stories'
import './placement.stories'
import './custom.stories'
import './tooltip.stories'

storiesOf('ToggleTip', module).add('Basic', () => {
return (
<div className="elements">
<div className="btn-group">
<div className="btn two">
<span>Net Price</span>
<ToggleTip content="Price of item before tax" />
</div>
<div className="btn two">
<span>Gross Price</span>
<ToggleTip content="Price of item including tax and service charges" />
</div>
</div>
</div>
)
})

storiesOf('ToggleTip', module).add('Placement', () => {
return (
<div className="elements">
<div className="btn-group">
<div className="btn">
<ToggleTip content="Show tooltip to Left" placement="left" />
<span>Left</span>
</div>
<div className="btn two">
<span>Top</span>
<ToggleTip content="Show tooltip to top" placement="top" />
</div>
<div className="btn three">
<span>Bottom</span>
<ToggleTip content="Show tooltip to bottom" placement="bottom" />
</div>
<div className="btn four">
<span>Right</span>
<ToggleTip content="Show tooltip to right" placement="right" />
</div>
</div>
</div>
)
})
29 changes: 29 additions & 0 deletions stories/placement.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {storiesOf} from '@storybook/react'
import React from 'react'
import ToggleTip from '../components/ToggleTip'

storiesOf('ToggleTip', module).add('Placement', () => {
return (
<div className="elements">
<div className="btn-group">
<div className="btn">
<ToggleTip content="Show tooltip to Left" placement="left" />
<span>Left</span>
</div>
<div className="btn two">
<span>Top</span>
<ToggleTip content="Show tooltip to top" placement="top" />
</div>
<div className="btn three">
<span>Bottom</span>
<ToggleTip content="Show tooltip to bottom" placement="bottom" />
</div>
<div className="btn four">
<span>Right</span>
<ToggleTip content="Show tooltip to right" placement="right" />
</div>
</div>
</div>
)
})

15 changes: 15 additions & 0 deletions stories/tooltip.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {storiesOf} from '@storybook/react'
import React from 'react'
import ToggleTip from '../components/ToggleTip'

storiesOf('ToggleTip', module).add('Tooltip', () => {
return (
<div className="elements">
<div className="btn-group">
<div className="btn two">
<ToggleTip content="This action will submit the page for real!" >SUBMIT</ToggleTip>
</div>
</div>
</div>
)
})

0 comments on commit 6cf97cc

Please sign in to comment.