-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Expand file tree
/
Copy pathlabel.tsx
More file actions
122 lines (104 loc) · 3 KB
/
label.tsx
File metadata and controls
122 lines (104 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import type { ComponentInterface, EventEmitter } from '@stencil/core';
import { Component, Element, Event, Host, Prop, State, Watch, h } from '@stencil/core';
import { createColorClasses, hostContext } from '@utils/theme';
import { getIonMode } from '../../global/ionic-global';
import type { Color, StyleEventDetail } from '../../interface';
/**
* @virtualProp {"ios" | "md"} mode - The mode determines which platform styles to use.
*/
@Component({
tag: 'ion-label',
styleUrls: {
ios: 'label.ios.scss',
md: 'label.md.scss',
},
scoped: true,
})
export class Label implements ComponentInterface {
private inRange = false;
private loadTimeout: ReturnType<typeof setTimeout> | undefined;
@Element() el!: HTMLElement;
/**
* The color to use from your application's color palette.
* Default options are: `"primary"`, `"secondary"`, `"tertiary"`, `"success"`, `"warning"`, `"danger"`, `"light"`, `"medium"`, and `"dark"`.
* For more information on colors, see [theming](/docs/theming/basics).
*/
@Prop({ reflect: true }) color?: Color;
/**
* The position determines where and how the label behaves inside an item.
*/
@Prop() position?: 'fixed' | 'stacked' | 'floating';
/**
* Emitted when the color changes.
* @internal
*/
@Event() ionColor!: EventEmitter<StyleEventDetail>;
/**
* Emitted when the styles change.
* @internal
*/
@Event() ionStyle!: EventEmitter<StyleEventDetail>;
@State() noAnimate = false;
componentWillLoad() {
this.inRange = !!this.el.closest('ion-range');
this.noAnimate = this.position === 'floating';
this.emitStyle();
this.emitColor();
}
componentDidLoad() {
if (this.noAnimate) {
this.loadTimeout = setTimeout(() => {
this.noAnimate = false;
}, 1000);
}
}
disconnectedCallback() {
if (this.loadTimeout) {
clearTimeout(this.loadTimeout);
}
}
@Watch('color')
colorChanged() {
this.emitColor();
}
@Watch('position')
positionChanged() {
this.emitStyle();
}
private emitColor() {
const { color } = this;
this.ionColor.emit({
'item-label-color': color !== undefined,
[`ion-color-${color}`]: color !== undefined,
});
}
private emitStyle() {
const { inRange, position } = this;
// If the label is inside of a range we don't want
// to override the classes added by the label that
// is a direct child of the item
if (!inRange) {
this.ionStyle.emit({
label: true,
[`label-${position}`]: position !== undefined,
});
}
}
render() {
const position = this.position;
const mode = getIonMode(this);
return (
<Host
class={createColorClasses(this.color, {
[mode]: true,
'in-item-color': hostContext('ion-item.ion-color', this.el),
[`label-${position}`]: position !== undefined,
[`label-no-animate`]: this.noAnimate,
'label-rtl': document.dir === 'rtl',
})}
>
<slot></slot>
</Host>
);
}
}