Skip to content
This repository was archived by the owner on Sep 4, 2020. It is now read-only.

Commit 1a3c8d6

Browse files
author
clouless
committed
fix onclick bug and move margin transition logic to theme
1 parent 27ea95a commit 1a3c8d6

File tree

5 files changed

+69
-37
lines changed

5 files changed

+69
-37
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"COMMENT": "THIS FILE WILL BE PARSED DURING BUILD. IT IS NOT THE ACTUAL PACKAGE FILE PUSHE TO NPMJS!",
33
"name": "@cloukit/tooltip",
44
"moduleId": "tooltip",
5-
"version": "1.1.0",
5+
"version": "1.2.0",
66
"description": "A simple tooltip component",
77
"license": "MIT",
88
"author": "codelcou.io",

src/components/children/tooltip.component.ts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,21 @@ export class CloukitTooltipComponent implements OnInit {
3434
public cloukitDropoutPlacement: DropoutPlacement;
3535

3636
@Input()
37-
public wrapperMargin: string;
37+
public wrapperReadyModifier: string;
3838

3939
@Input()
4040
public theme: string;
4141

4242
private themeSelected: CloukitComponentTheme;
4343
private state = {
44-
uiModifier: 'base',
45-
uiState: 'init',
46-
tooltipTransform: '',
44+
wrapper: {
45+
uiModifier: 'base',
46+
uiState: 'init',
47+
},
48+
tooltip: {
49+
uiModifier: 'base',
50+
uiState: 'init',
51+
}
4752
};
4853

4954
constructor(private themeService: CloukitThemeService) {
@@ -52,36 +57,27 @@ export class CloukitTooltipComponent implements OnInit {
5257

5358
public getStyle(element: string): CloukitStatefulAndModifierAwareElementThemeStyleDefinition {
5459
if (this.themeSelected !== undefined && this.themeSelected !== null) {
55-
const style = this.themeSelected.getStyle(element, this.state.uiState, this.state.uiModifier);
56-
if (element === 'tooltip') {
57-
style.style[ 'transform' ] = this.state.tooltipTransform;
58-
}
59-
if (element === 'wrapper') {
60-
style.style[this.wrapperMargin] = this.state.uiState === 'ready' ? '5px' : '0px';
61-
}
60+
const style = this.themeSelected.getStyle(element, this.state[element].uiState, this.state[element].uiModifier);
6261
return this.themeService.prefixStyle(style);
6362
}
6463
return { style: {}, icon: {} } as CloukitStatefulAndModifierAwareElementThemeStyleDefinition;
6564
}
6665

6766
ngOnInit() {
6867
const self = this;
69-
if (this.cloukitDropoutPlacement === DropoutPlacement.DOWN_CENTER || this.cloukitDropoutPlacement === DropoutPlacement.UP_CENTER ) {
70-
this.state.tooltipTransform = 'translate(-50%, 0)';
71-
}
72-
if (this.cloukitDropoutPlacement === DropoutPlacement.LEFT_CENTER || this.cloukitDropoutPlacement === DropoutPlacement.RIGHT_CENTER ) {
73-
this.state.tooltipTransform = 'translate(0, -50%)';
74-
}
7568
if (this.theme !== undefined && this.theme !== null) {
7669
this.themeSelected = this.themeService.getComponentTheme(this.theme);
7770
if (this.themeSelected === null) {
7871
console.log(`WARN: requested theme ${this.theme} does not exist. Falling back to default theme for tooltip.`);
7972
this.themeSelected = this.themeService.getComponentTheme('tooltip');
8073
}
8174
}
82-
// Transition to ready state once component is created
75+
76+
// Transition to ready state slightly after component is created
8377
setTimeout(() => {
84-
self.state.uiState = 'ready';
78+
self.state.tooltip.uiState = 'ready';
79+
self.state.wrapper.uiState = 'ready';
80+
self.state.wrapper.uiModifier = this.wrapperReadyModifier;
8581
}, 10)
8682
}
8783

src/components/tooltip.directive.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,18 @@ export class CloukitTooltipDirective {
3232
// PLACEMENT
3333
//
3434
let placement: DropoutPlacement;
35-
let wrapperMargin: string;
35+
let wrapperReadyModifier: string;
3636
if (this.cloukitDropoutPlacement === undefined || this.cloukitDropoutPlacement === 'bottom') {
3737
placement = DropoutPlacement.DOWN_CENTER;
38-
wrapperMargin = 'marginTop';
38+
wrapperReadyModifier = 'down';
3939
} else if (this.cloukitDropoutPlacement === 'top') {
40-
wrapperMargin = 'marginBottom';
40+
wrapperReadyModifier = 'up';
4141
placement = DropoutPlacement.UP_CENTER;
4242
} else if (this.cloukitDropoutPlacement === 'left') {
43-
wrapperMargin = 'marginRight';
43+
wrapperReadyModifier = 'left';
4444
placement = DropoutPlacement.LEFT_CENTER;
4545
} else if (this.cloukitDropoutPlacement === 'right') {
46-
wrapperMargin = 'marginLeft';
46+
wrapperReadyModifier = 'right';
4747
placement = DropoutPlacement.RIGHT_CENTER;
4848
}
4949
//
@@ -53,7 +53,7 @@ export class CloukitTooltipDirective {
5353
const tooltipRef = this.viewContainerRef.createComponent(componentFactory);
5454
tooltipRef.instance.tooltipText = this.cloukitDropout;
5555
tooltipRef.instance.cloukitDropoutPlacement = placement;
56-
tooltipRef.instance.wrapperMargin = wrapperMargin;
56+
tooltipRef.instance.wrapperReadyModifier = wrapperReadyModifier;
5757
//
5858
// REQUEST
5959
//
@@ -69,13 +69,11 @@ export class CloukitTooltipDirective {
6969
this.dropoutRef = undefined;
7070
}
7171

72-
@HostListener('focusin')
7372
@HostListener('mouseenter')
7473
activate() {
7574
this._doActivate();
7675
}
7776

78-
@HostListener('focusout')
7977
@HostListener('mouseleave')
8078
deactivate() {
8179
this._doDeactivate();

src/components/tooltip.theme.ts

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,63 @@ export class CloukitTooltipComponentThemeDefault extends CloukitComponentTheme {
1616
constructor() {
1717
super();
1818
//
19-
// NOTE: A margin-* of 5px will be added for wrapper.ready.base on component creation.
20-
// this cannot be overridden by the theme! Since the placement influences top/bottom/left/right
19+
// WRAPPER
2120
//
2221
this.createStyle('wrapper', 'init', 'base', {
2322
style: {
2423
transition: `
25-
margin-left 200ms linear,
26-
margin-right 200ms linear,
27-
margin-bottom 200ms linear,
28-
margin-top 200ms linear,
29-
opacity 200ms ease-in-out`,
24+
margin-left 300ms linear,
25+
margin-right 300ms linear,
26+
margin-top 300ms linear,
27+
margin-bottom 300ms linear,
28+
opacity 300ms ease-in-out`,
3029
opacity: 0,
3130
}
3231
} as CloukitStatefulAndModifierAwareElementThemeStyleDefinition);
3332

34-
this.createStyle('wrapper', 'ready', 'base',
33+
this.createStyle('wrapper', 'ready', 'left',
3534
this.merge(this.getStyle('wrapper', 'init', 'base'), {
3635
style: {
36+
marginRight: '5px',
37+
transform: 'translate(0, -50%)',
3738
opacity: 1,
3839
}
3940
} as CloukitStatefulAndModifierAwareElementThemeStyleDefinition));
4041

42+
this.createStyle('wrapper', 'ready', 'right',
43+
this.merge(this.getStyle('wrapper', 'init', 'base'), {
44+
style: {
45+
marginLeft: '5px',
46+
transform: 'translate(0, -50%)',
47+
opacity: 1,
48+
}
49+
} as CloukitStatefulAndModifierAwareElementThemeStyleDefinition));
50+
51+
this.createStyle('wrapper', 'ready', 'up',
52+
this.merge(this.getStyle('wrapper', 'init', 'base'), {
53+
style: {
54+
marginBottom: '5px',
55+
transform: 'translate(-50%, 0)',
56+
opacity: 1,
57+
}
58+
} as CloukitStatefulAndModifierAwareElementThemeStyleDefinition));
59+
60+
this.createStyle('wrapper', 'ready', 'down',
61+
this.merge(this.getStyle('wrapper', 'init', 'base'), {
62+
style: {
63+
marginTop: '5px',
64+
transform: 'translate(-50%, 0)',
65+
opacity: 1,
66+
}
67+
} as CloukitStatefulAndModifierAwareElementThemeStyleDefinition));
68+
69+
//
70+
// TOOLTIP
71+
//
4172
this.createStyle('tooltip', 'init', 'base', {
4273
style: {
4374
backgroundColor: '#333',
44-
padding: '4px',
75+
padding: '5px 8px 5px 8px',
4576
color: '#fff',
4677
}
4778
} as CloukitStatefulAndModifierAwareElementThemeStyleDefinition);

src/demo/demo.component.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,11 @@
2828
cloukitTooltip="delicious german sausage"
2929
cloukitTooltipPlacement="bottom"
3030
>Bratwurst</span>
31+
<br><br>
32+
<span class="label">tooltip on a button:</span>
33+
<button
34+
class="tooltipTrigger"
35+
cloukitTooltip="delicious german sausage"
36+
cloukitTooltipPlacement="bottom"
37+
>Bratwurst</button>
3138
</div>

0 commit comments

Comments
 (0)