-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtext.vue
More file actions
345 lines (297 loc) · 9.02 KB
/
text.vue
File metadata and controls
345 lines (297 loc) · 9.02 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<template>
<component
:is="as"
data-qa="dt-text"
:class="textClasses"
:style="textStyles"
>
<!-- @slot Default slot for text content -->
<slot />
</component>
</template>
<script>
import {
TEXT_KIND_MODIFIERS,
TEXT_SIZE_MODIFIERS,
TEXT_SIZE_MAP,
TEXT_HEADLINE_ONLY_SIZES,
TEXT_ALIGN_MODIFIERS,
TEXT_TONE_MODIFIERS,
TEXT_NUMERIC_CLASS,
TEXT_TRUNCATE_CLASS,
TEXT_LINE_CLAMP_CLASS,
TEXT_WRAP_MODIFIERS,
TEXT_BOX_TRIM_MODIFIERS,
TEXT_STRENGTH_MODIFIERS,
TEXT_DENSITY_MODIFIERS,
} from './text_constants';
const DEFAULT_SIZE = '300';
const SEMANTIC_HEADING_ELEMENTS = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
// Module-level flag to emit info only once per session
let hasEmittedHeadlineSemanticInfo = false;
// Exported for testing purposes only
export const resetHeadlineSemanticInfoFlag = () => {
hasEmittedHeadlineSemanticInfo = false;
};
/**
* Dialtone text primitive that applies typography tokens based on semantic props.
* @see https://dialtone.dialpad.com/components/text.html
*/
export default {
compatConfig: { MODE: 3 },
name: 'DtText',
props: {
/**
* HTML tag or component used for rendering.
* @values span, p, h1, h2, h3, h4, h5, h6, label, div, component
*/
as: {
type: String,
default: 'span',
},
/**
* Typography kind mapping to headline/body/label/code token sets.
* @values headline, body, label, code
*/
kind: {
type: String,
default: null,
validator: (value) => {
return value === null || Object.prototype.hasOwnProperty.call(TEXT_KIND_MODIFIERS, value);
},
},
/**
* Size variant within the selected `kind`. Falls back to `md`/300 if unsupported.
* Headline supports all sizes; body/label/code support 100-400.
* @values 100, 200, 300, 400, 500, 600, 700
*/
size: {
type: [String, Number],
default: null,
},
/**
* Semantic foreground color.
* @values primary, secondary, tertiary, muted, disabled, placeholder, critical, critical-strong, positive, positive-strong, success, success-strong, warning, neutral-black, neutral-white
*/
tone: {
type: String,
default: null,
validator: (value) => {
return value === null || Object.prototype.hasOwnProperty.call(TEXT_TONE_MODIFIERS, value);
},
},
/**
* Logical text alignment. Requires block/inline-block context.
* @values start, center, end, justify
*/
align: {
type: String,
default: null,
validator: (value) => {
return value === null || Object.prototype.hasOwnProperty.call(TEXT_ALIGN_MODIFIERS, value);
},
},
/**
* Enables single-line truncation (i.e. ellipsis) when true; requires block/inline-block context.
*/
truncate: {
type: Boolean,
default: false,
},
/**
* Applies multi-line truncation (i.e. clamp) when greater than zero; requires block/inline-block context.
*/
maxLines: {
type: Number,
default: null,
validator: (value) => {
return value === null || (Number.isInteger(value) && value > 0);
},
},
/**
* Renders numeric content with tabular figures.
*/
numeric: {
type: Boolean,
default: false,
},
/**
* Controls text wrapping behavior.
* @values wrap, nowrap, balance, pretty
*/
wrap: {
type: String,
default: null,
validator: (value) => {
return value === null || Object.prototype.hasOwnProperty.call(TEXT_WRAP_MODIFIERS, value);
},
},
/**
* Controls text-box-trim (leading space above/below text). Useful for tight component layouts.
* @values start, end, both
*/
textBoxTrim: {
type: String,
default: null,
validator: (value) => {
return value === null || Object.prototype.hasOwnProperty.call(TEXT_BOX_TRIM_MODIFIERS, value);
},
},
/**
* Overrides font-weight. Applies to any kind/size combination.
* @values bold, semibold, medium, normal
*/
strength: {
type: String,
default: null,
validator: (value) => {
return value === null || Object.prototype.hasOwnProperty.call(TEXT_STRENGTH_MODIFIERS, value);
},
},
/**
* Overrides line-height. Applies to any kind/size combination.
* @values 100, 200, 300, 400, 500, 600
*/
density: {
type: [String, Number],
default: null,
validator: (value) => {
return value === null || Object.prototype.hasOwnProperty.call(TEXT_DENSITY_MODIFIERS, value);
},
},
},
computed: {
textClasses () {
const classes = ['d-text'];
const variantClass = this.getVariantClass();
if (variantClass) {
classes.push(variantClass);
}
const alignClass = this.getAlignClass();
if (alignClass) {
classes.push(alignClass);
}
const toneClass = this.getToneClass();
if (toneClass) {
classes.push(toneClass);
}
if (this.truncate) {
classes.push(TEXT_TRUNCATE_CLASS);
}
if (this.numeric) {
classes.push(TEXT_NUMERIC_CLASS);
}
if (this.maxLines) {
classes.push(TEXT_LINE_CLAMP_CLASS);
}
const wrapClass = this.getWrapClass();
if (wrapClass) {
classes.push(wrapClass);
}
const textBoxTrimClass = this.getTextBoxTrimClass();
if (textBoxTrimClass) {
classes.push(textBoxTrimClass);
}
const strengthClass = this.getStrengthClass();
if (strengthClass) {
classes.push(strengthClass);
}
const densityClass = this.getDensityClass();
if (densityClass) {
classes.push(densityClass);
}
return classes;
},
textStyles () {
if (!this.maxLines) {
return undefined;
}
return {
'--dt-text-line-clamp': this.maxLines,
};
},
},
mounted () {
// Emit info once per session when headline is used without semantic heading element
if (
!hasEmittedHeadlineSemanticInfo &&
this.kind === 'headline' &&
!SEMANTIC_HEADING_ELEMENTS.includes(this.as)
) {
console.info(
'[DtText] kind="headline" is used without a semantic heading element. ' +
'Consider using as="h1|h2|h3|h4|h5|h6" for better accessibility.',
);
hasEmittedHeadlineSemanticInfo = true;
}
},
methods: {
getVariantClass () {
if (!this.kind) {
if (this.size) {
console.warn('[DtText] size prop has no effect without kind. Set kind="headline|body|label|code".');
}
return null;
}
if (!Object.prototype.hasOwnProperty.call(TEXT_KIND_MODIFIERS, this.kind)) {
console.warn(`[DtText] Unsupported kind "${this.kind}".`);
return null;
}
const allowedSizes = TEXT_SIZE_MODIFIERS[this.kind] || [];
const rawSize = this.size != null ? String(this.size) : null;
const requestedSize = rawSize || DEFAULT_SIZE;
let resolvedSize = requestedSize;
if (!allowedSizes.includes(requestedSize)) {
// Headline-only sizes (3xl, 2xl, xl, 500, 600, 700) throw an error when used with incompatible kinds
if (TEXT_HEADLINE_ONLY_SIZES.includes(requestedSize)) {
throw new Error(
`[DtText] size="${requestedSize}" is only valid for kind="headline". ` +
`Cannot use with kind="${this.kind}".`,
);
}
// Universal sizes fall back gracefully with a warning
const fallbackSize = allowedSizes.includes(DEFAULT_SIZE) ? DEFAULT_SIZE : allowedSizes[0];
if (fallbackSize) {
resolvedSize = fallbackSize;
}
console.warn(`[DtText] size="${requestedSize}" is not valid for kind="${this.kind}". Using "${resolvedSize}" instead.`);
}
if (!resolvedSize) {
return null;
}
// Map numeric sizes to t-shirt CSS class suffix
const cssSuffix = TEXT_SIZE_MAP[resolvedSize] || resolvedSize;
return `${TEXT_KIND_MODIFIERS[this.kind]}--${cssSuffix}`;
},
getModifierClass (value, modifiers, propName) {
if (value === null || value === undefined) {
return null;
}
const className = modifiers[value];
if (!className) {
console.warn(`[DtText] Unsupported ${propName} "${value}".`);
return null;
}
return className;
},
getAlignClass () {
return this.getModifierClass(this.align, TEXT_ALIGN_MODIFIERS, 'align');
},
getToneClass () {
return this.getModifierClass(this.tone, TEXT_TONE_MODIFIERS, 'tone');
},
getWrapClass () {
return this.getModifierClass(this.wrap, TEXT_WRAP_MODIFIERS, 'wrap');
},
getTextBoxTrimClass () {
return this.getModifierClass(this.textBoxTrim, TEXT_BOX_TRIM_MODIFIERS, 'textBoxTrim');
},
getStrengthClass () {
return this.getModifierClass(this.strength, TEXT_STRENGTH_MODIFIERS, 'strength');
},
getDensityClass () {
return this.getModifierClass(this.density, TEXT_DENSITY_MODIFIERS, 'density');
},
},
};
</script>