Skip to content

Commit 125be23

Browse files
authored
feat: Add position prop (#5)
1 parent 488a972 commit 125be23

File tree

3 files changed

+128
-135
lines changed

3 files changed

+128
-135
lines changed

dev/src/App.svelte

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<script>
22
import { useTooltip } from '../../src'
33
4+
let tooltipPosition = 'top'
45
let useCustomTooltipClass = false
5-
let animateTooltip = false
6+
let isTooltipDisabled = false
67
78
const _onTooltipClick = (arg, event) => {
89
console.log(arg)
@@ -12,50 +13,46 @@
1213
<main>
1314
<div class="container">
1415
<div use:useTooltip={{
15-
contentSelector: '.tooltip__content',
16+
position: tooltipPosition,
17+
contentSelector: '.tooltip__button',
1618
contentClone: false,
1719
contentActions: {
18-
'#button1': {
19-
eventType: 'mouseenter',
20-
callback: (arg) => console.log(arg),
21-
callbackParams: ['Haha you\'re hovering the button 1'],
22-
closeOnCallback: false
23-
},
24-
'#button2': {
25-
eventType: 'mouseenter',
26-
callback: (arg1, arg2) => console.log(arg1, arg2),
27-
callbackParams: ['Haha you\'re hovering the', 'button 2'],
28-
closeOnCallback: false
29-
},
30-
'#button2': {
31-
eventType: 'click',
32-
callback: (arg1, arg2) => console.log(arg1, arg2),
33-
callbackParams: ['Haha you\'ve clicked the', 'button 2'],
34-
closeOnCallback: true
35-
},
20+
'*': {
21+
eventType: 'click',
22+
callback: _onTooltipClick,
23+
callbackParams: ['ok'],
24+
closeOnCallback: true
25+
},
3626
},
3727
contentClassName: useCustomTooltipClass ? 'tooltip' : null,
38-
disabled: false,
39-
animated: animateTooltip
40-
}} class="tooltip__target">Hover me</div>
41-
<span class="tooltip__content">
42-
<button id="button1">Action 1</button>
43-
<button id="button2">Action 2</button>
44-
</span>
28+
disabled: isTooltipDisabled,
29+
}} class="target">Hover me</div>
30+
<span class="tooltip__button">Hi! I'm a fancy tooltip!</span>
4531
<form class="settings__form">
4632
<h1>Settings</h1>
33+
<fieldset>
34+
<label>
35+
Use Custom Tooltip Class:
36+
<input type="checkbox" bind:checked={useCustomTooltipClass} />
37+
</label>
38+
</fieldset>
4739
<fieldset>
4840
<label>
49-
Use Custom Tooltip Class:
50-
<input type="checkbox" bind:checked={useCustomTooltipClass} />
51-
</label>
52-
</fieldset>
53-
<fieldset>
54-
<label>
55-
Animate tooltip:
56-
<input type="checkbox" bind:checked={animateTooltip} />
41+
Tooltip Position:
42+
<select bind:value={tooltipPosition}>
43+
<option value="left">Left</option>
44+
<option value="right">Right</option>
45+
<option value="top">Top</option>
46+
<option value="bottom">Bottom</option>
47+
</select>
5748
</label>
5849
</fieldset>
50+
<fieldset>
51+
<label>
52+
Disable Tooltip:
53+
<input type="checkbox" bind:checked={isTooltipDisabled} />
54+
</label>
55+
</fieldset>
5956
</form>
6057
</div>
6158
</main>
@@ -76,7 +73,7 @@
7673
row-gap: 1rem;
7774
}
7875
79-
.tooltip__target {
76+
.target {
8077
width: 10rem;
8178
height: 3rem;
8279
background-color: white;
@@ -87,16 +84,12 @@
8784
box-shadow: 0 0 5px 0 rgba(0,0,0,0.5);
8885
}
8986
90-
.tooltip__target:hover {
87+
.target:hover {
9188
cursor: pointer;
9289
background-color: black;
9390
color: white;
9491
}
9592
96-
.tooltip__content {
97-
color: white
98-
}
99-
10093
.settings__form {
10194
display: flex;
10295
flex-direction: column;

src/useTooltip.css

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.__tooltip__default {
22
position: absolute;
33
z-index: 9999;
4-
max-width: 120px;
4+
max-width: 100%;
55
background-color: black;
66
color: #fff;
77
text-align: center;
@@ -12,10 +12,31 @@
1212
.__tooltip__default::after {
1313
content: "";
1414
position: absolute;
15-
top: 100%;
16-
left: 50%;
1715
margin-left: -5px;
1816
border-width: 5px;
1917
border-style: solid;
18+
}
19+
20+
.__tooltip__top::after {
21+
bottom: -10px;
22+
left: 50%;
2023
border-color: black transparent transparent transparent;
24+
}
25+
26+
.__tooltip__bottom::after {
27+
top: -10px;
28+
left: 50%;
29+
border-color: transparent transparent black transparent;
30+
}
31+
32+
.__tooltip__left::after {
33+
top: calc(50% - 5px);
34+
right: -10px;
35+
border-color: transparent transparent transparent black;
36+
}
37+
38+
.__tooltip__right::after {
39+
top: calc(50% - 5px);
40+
left: -5px;
41+
border-color: transparent black transparent transparent;
2142
}

0 commit comments

Comments
 (0)