|
1 | 1 | <p align="center"> |
2 | | - svelte-use-tooltip |
| 2 | + <img src="assets/svelte-use-tooltip.png" alt="svelte-use-tooltip" height="100"/> |
3 | 3 | </p> |
4 | 4 | <p align="center"> |
5 | 5 | Svelte action to display a tooltip |
6 | 6 | </p> |
7 | 7 |
|
8 | | -TODO |
| 8 | +--- |
| 9 | + |
| 10 | +[](https://www.npmjs.com/package/@untemps/svelte-use-tooltip) |
| 11 | +[](https://github.com/untemps/svelte-use-tooltip/actions) |
| 12 | +[](https://codecov.io/gh/untemps/svelte-use-tooltip) |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +```bash |
| 17 | +yarn add @untemps/svelte-use-tooltip |
| 18 | +``` |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +### Basic usage |
| 23 | + |
| 24 | +```sveltehtml |
| 25 | +<script> |
| 26 | + import { useTooltip } from '@untemps/svelte-use-tooltip' |
| 27 | +
|
| 28 | + const _onTooltipClick = (arg, event) => { |
| 29 | + console.log(arg, event) |
| 30 | + } |
| 31 | +</script> |
| 32 | +
|
| 33 | +<div use:useTooltip={{ |
| 34 | + contentSelector: '.tooltip__content', |
| 35 | + contentClone: false, |
| 36 | + contentActions: { |
| 37 | + '*': { |
| 38 | + eventType: 'click', |
| 39 | + callback: _onTooltipClick, |
| 40 | + callbackParams: ['ok'], |
| 41 | + closeOnCallback: true |
| 42 | + }, |
| 43 | + }, |
| 44 | + contentClassName: 'tooltip', |
| 45 | + disabled: false, |
| 46 | + animated: true |
| 47 | + } |
| 48 | +} class="tooltip__target"> |
| 49 | + Hover me |
| 50 | +</div> |
| 51 | +<span class="tooltip__content">Hi!</span> |
| 52 | +
|
| 53 | +<style> |
| 54 | + .tooltip__target { |
| 55 | + width: 10rem; |
| 56 | + height: 3rem; |
| 57 | + background-color: white; |
| 58 | + color: black; |
| 59 | + display: flex; |
| 60 | + align-items: center; |
| 61 | + justify-content: center; |
| 62 | + box-shadow: 0 0 5px 0 rgba(0,0,0,0.5); |
| 63 | + } |
| 64 | +
|
| 65 | + .tooltip__target:hover { |
| 66 | + cursor: pointer; |
| 67 | + background-color: black; |
| 68 | + color: white; |
| 69 | + } |
| 70 | +
|
| 71 | + .tooltip__content { |
| 72 | + border: solid 1px white; |
| 73 | + background: none; |
| 74 | + } |
| 75 | +
|
| 76 | + :global(.tooltip) { |
| 77 | + position: absolute; |
| 78 | + z-index: 9999; |
| 79 | + max-width: 120px; |
| 80 | + background-color: #ee7008; |
| 81 | + color: #fff; |
| 82 | + text-align: center; |
| 83 | + border-radius: 6px; |
| 84 | + padding: 0.5rem; |
| 85 | + } |
| 86 | + |
| 87 | + :global(.tooltip::after) { |
| 88 | + content: ''; |
| 89 | + position: absolute; |
| 90 | + top: 100%; |
| 91 | + left: 50%; |
| 92 | + margin-left: -5px; |
| 93 | + border-width: 5px; |
| 94 | + border-style: solid; |
| 95 | + border-color: #ee7008 transparent transparent transparent; |
| 96 | + } |
| 97 | +</style> |
| 98 | +``` |
| 99 | + |
| 100 | +## API |
| 101 | + |
| 102 | +| Props | Type | Default | Description | |
| 103 | +|----------------------|---------|---------|-----------------------------------------------------------------------------------------------------------------| |
| 104 | +| `contentSelector` | string | null | Selector of the content to display in the tooltip. | |
| 105 | +| `contentClone` | boolean | null | Flag to clone the content to display in the tooltip. If false, the content is removed from its previous parent. | |
| 106 | +| `contentActions` | object | null | Configuration of the tooltip actions (see [Content Actions](#content-actions)). | |
| 107 | +| `containerClassName` | string | null | Class name to apply to the tooltip container. | |
| 108 | +| `disabled` | boolean | false | Flag to disable the tooltip content. | |
| 109 | +| `animated` | boolean | false | Flag to animate tooltip transitions. | |
| 110 | + |
| 111 | +### Content Actions |
| 112 | + |
| 113 | +The `contentActions` prop allow handling interactions within the tooltip content. |
| 114 | + |
| 115 | +Each element inside the content parent may configure its own action since it can be queried using the key-selector. |
| 116 | + |
| 117 | +One event by element is possible so far as elements are referenced by selector. The last one declared in the `contentActions` object has precedence over the previous ones. |
| 118 | + |
| 119 | +```sveltehtml |
| 120 | +<script> |
| 121 | + import { useTooltip } from '@untemps/svelte-use-tooltip' |
| 122 | +</script> |
| 123 | +
|
| 124 | +<div use:useTooltip={{ |
| 125 | + contentSelector: '#content', |
| 126 | + contentClone: false, |
| 127 | + contentActions: { |
| 128 | + '#button1': { |
| 129 | + eventType: 'mouseenter', |
| 130 | + callback: (arg) => console.log(arg), |
| 131 | + callbackParams: ['Haha you\'re hovering the button 1'], |
| 132 | + closeOnCallback: true |
| 133 | + }, |
| 134 | + '#button2': { |
| 135 | + eventType: 'click', |
| 136 | + callback: (arg1, arg2) => console.log(arg1, arg2), |
| 137 | + callbackParams: ['Haha you\'ve clicked the', 'button 2'], |
| 138 | + closeOnCallback: false |
| 139 | + }, |
| 140 | + } |
| 141 | +}}>Hover me</div> |
| 142 | +<span id="content"> |
| 143 | + <button id="button1">Action 1</button> |
| 144 | + <button id="button2">Action 2</button> |
| 145 | +</span> |
| 146 | +``` |
| 147 | + |
| 148 | +| Props | Type | Default | Description | |
| 149 | +|-------------------|----------|---------|----------------------------------------------------------------------------------------------------------| |
| 150 | +| `eventType` | string | null | Type of the event. All available [events](https://developer.mozilla.org/fr/docs/Web/Events) can be used. | |
| 151 | +| `callback` | function | null | Function to be used as event handler. | |
| 152 | +| `callbackParams` | array | null | List of arguments to pass to the event handler in. | |
| 153 | +| `closeOnCallback` | boolean | false | Flag to automatically close the tooltip when the event handler is triggered. | | |
| 154 | + |
| 155 | +## Development |
| 156 | + |
| 157 | +The component can be served for development purpose on `http://localhost:5000/` running: |
| 158 | + |
| 159 | +``` |
| 160 | +yarn dev |
| 161 | +``` |
| 162 | + |
| 163 | +## Contributing |
| 164 | + |
| 165 | +Contributions are warmly welcomed: |
| 166 | + |
| 167 | +- Fork the repository |
| 168 | +- Create a feature branch |
| 169 | +- Develop the feature AND write the tests (or write the tests AND develop the feature) |
| 170 | +- Commit your changes |
| 171 | + using [Angular Git Commit Guidelines](https://github.com/angular/angular.js/blob/master/DEVELOPERS.md#-git-commit-guidelines) |
| 172 | +- Submit a Pull Request |
0 commit comments