-
Notifications
You must be signed in to change notification settings - Fork 513
/
feature-row.tsx
581 lines (546 loc) · 16.1 KB
/
feature-row.tsx
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
import React, { useContext } from "react";
import type BCD from "@mdn/browser-compat-data/types";
import { BrowserInfoContext } from "./browser-info";
import {
asList,
getCurrentSupport,
getPreviousVersion,
hasMore,
hasNoteworthyNotes,
isFullySupportedWithoutLimitation,
isNotSupportedAtAll,
isTruthy,
versionIsPreview,
SupportStatementExtended,
} from "./utils";
import { LEGEND_LABELS } from "./legend";
import { CompatStatementExtended } from "../../../../../libs/types";
function getSupportClassName(
support: SupportStatementExtended | undefined,
browser: BCD.BrowserStatement
): "no" | "yes" | "partial" | "preview" | "removed-partial" | "unknown" {
if (!support) {
return "unknown";
}
let { flags, version_added, version_removed, partial_implementation } =
getCurrentSupport(support)!;
let className;
if (version_added === null) {
className = "unknown";
} else if (versionIsPreview(version_added, browser)) {
className = "preview";
} else if (version_added) {
className = "yes";
if (version_removed || (flags && flags.length)) {
className = "no";
}
} else {
className = "no";
}
if (partial_implementation) {
className = version_removed ? "removed-partial" : "partial";
}
return className;
}
function getSupportBrowserReleaseDate(
support: SupportStatementExtended | undefined
): string | undefined {
if (!support) {
return undefined;
}
return getCurrentSupport(support)!.release_date;
}
function StatusIcons({ status }: { status: BCD.StatusBlock }) {
const icons = [
status.experimental && {
title: "Experimental. Expect behavior to change in the future.",
text: "Experimental",
iconClassName: "icon-experimental",
},
status.deprecated && {
title: "Deprecated. Not for use in new websites.",
text: "Deprecated",
iconClassName: "icon-deprecated",
},
!status.standard_track && {
title: "Non-standard. Expect poor cross-browser support.",
text: "Non-standard",
iconClassName: "icon-nonstandard",
},
].filter(isTruthy);
return icons.length === 0 ? null : (
<div className="bc-icons" data-test={icons.length}>
{icons.map((icon) => (
<abbr
key={icon.iconClassName}
className={`only-icon icon ${icon.iconClassName}`}
title={icon.title}
>
<span>{icon.text}</span>
</abbr>
))}
</div>
);
}
function labelFromString(
version: string | boolean | null | undefined,
browser: BCD.BrowserStatement
) {
if (typeof version !== "string") {
return <>{"?"}</>;
}
// Treat BCD ranges as exact versions to avoid confusion for the reader
// See https://github.com/mdn/yari/issues/3238
if (version.startsWith("≤")) {
return <>{version.slice(1)}</>;
}
if (version === "preview") {
return browser.preview_name;
}
return <>{version}</>;
}
function versionLabelFromSupport(
added: string | boolean | null | undefined,
removed: string | boolean | null | undefined,
browser: BCD.BrowserStatement
) {
if (typeof removed !== "string") {
return <>{labelFromString(added, browser)}</>;
}
return (
<>
{labelFromString(added, browser)} – 
{labelFromString(removed, browser)}
</>
);
}
const CellText = React.memo(
({
support,
browser,
timeline = false,
}: {
support: BCD.SupportStatement | undefined;
browser: BCD.BrowserStatement;
timeline?: boolean;
}) => {
const currentSupport = getCurrentSupport(support);
const added = currentSupport?.version_added ?? null;
const removed = getPreviousVersion(
currentSupport?.version_removed ?? null,
browser
);
const browserReleaseDate = getSupportBrowserReleaseDate(support);
const supportClassName = getSupportClassName(support, browser);
let status:
| { isSupported: "unknown" }
| {
isSupported: "no" | "yes" | "partial" | "preview" | "removed-partial";
label?: React.ReactNode;
};
switch (added) {
case null:
status = { isSupported: "unknown" };
break;
case true:
status = { isSupported: removed ? "no" : "yes" };
break;
case false:
status = { isSupported: "no" };
break;
case "preview":
status = { isSupported: "preview" };
break;
default:
status = {
isSupported: supportClassName,
label: versionLabelFromSupport(added, removed, browser),
};
break;
}
let label: string | React.ReactNode;
let title = "";
switch (status.isSupported) {
case "yes":
title = "Full support";
label = status.label || "Yes";
break;
case "partial":
title = "Partial support";
label = status.label || "Partial";
break;
case "removed-partial":
if (timeline) {
title = "Partial support";
label = status.label || "Partial";
} else {
title = "No support";
label = status.label || "No";
}
break;
case "no":
title = "No support";
label = status.label || "No";
break;
case "preview":
title = "Preview browser support";
label = status.label || browser.preview_name;
break;
case "unknown":
title = "Compatibility unknown; please update this.";
label = "?";
break;
}
return (
<div className="bcd-cell-text-wrapper">
<div className="bcd-cell-icons">
<span className="icon-wrap">
<abbr
className={`
bc-level-${supportClassName}
icon
icon-${supportClassName}`}
title={title}
>
<span className="bc-support-level">{title}</span>
</abbr>
</span>
</div>
<div className="bcd-cell-text-copy">
<span className="bc-browser-name">{browser.name}</span>
<span
className="bc-version-label"
title={
browserReleaseDate ? `Released ${browserReleaseDate}` : undefined
}
>
{label}
</span>
</div>
<CellIcons support={support} />
</div>
);
}
);
function Icon({ name }: { name: string }) {
const title = LEGEND_LABELS[name] ?? name;
return (
<abbr className="only-icon" title={title}>
<span>{name}</span>
<i className={`icon icon-${name}`} />
</abbr>
);
}
function CellIcons({ support }: { support: BCD.SupportStatement | undefined }) {
const supportItem = getCurrentSupport(support);
if (!supportItem) {
return null;
}
const icons = [
supportItem.prefix && <Icon key="prefix" name="prefix" />,
hasNoteworthyNotes(supportItem) && <Icon key="footnote" name="footnote" />,
supportItem.alternative_name && <Icon key="altname" name="altname" />,
supportItem.flags && <Icon key="disabled" name="disabled" />,
hasMore(support) && <Icon key="more" name="more" />,
].filter(Boolean);
return icons.length ? <div className="bc-icons">{icons}</div> : null;
}
function FlagsNote({
supportItem,
browser,
}: {
supportItem: BCD.SimpleSupportStatement;
browser: BCD.BrowserStatement;
}) {
const hasAddedVersion = typeof supportItem.version_added === "string";
const hasRemovedVersion = typeof supportItem.version_removed === "string";
const flags = supportItem.flags || [];
return (
<>
{hasAddedVersion && `From version ${supportItem.version_added}`}
{hasRemovedVersion && (
<>
{hasAddedVersion ? " until" : "Until"} version{" "}
{supportItem.version_removed} (exclusive)
</>
)}
{hasAddedVersion || hasRemovedVersion ? ": this" : "This"} feature is
behind the
{flags.map((flag, i) => {
const valueToSet = flag.value_to_set && (
<>
{" "}
(needs to be set to <code>{flag.value_to_set}</code>)
</>
);
return (
<React.Fragment key={flag.name}>
<code>{flag.name}</code>
{flag.type === "preference" && <> preferences{valueToSet}</>}
{flag.type === "runtime_flag" && <> runtime flag{valueToSet}</>}
{i < flags.length - 1 && " and the "}
</React.Fragment>
);
})}
.
{browser.pref_url &&
flags.some((flag) => flag.type === "preference") &&
` To change preferences in ${browser.name}, visit ${browser.pref_url}.`}
</>
);
}
function getNotes(
browser: BCD.BrowserStatement,
support: BCD.SupportStatement
) {
if (support) {
return asList(support)
.slice()
.reverse()
.flatMap((item, i) => {
const supportNotes = [
item.version_removed &&
!asList(support).some(
(otherItem) => otherItem.version_added === item.version_removed
)
? {
iconName: "disabled",
label: (
<>
Removed in {labelFromString(item.version_removed, browser)}{" "}
and later
</>
),
}
: null,
item.partial_implementation
? {
iconName: "footnote",
label: "Partial support",
}
: null,
item.prefix
? {
iconName: "prefix",
label: `Implemented with the vendor prefix: ${item.prefix}`,
}
: null,
item.alternative_name
? {
iconName: "altname",
label: `Alternate name: ${item.alternative_name}`,
}
: null,
item.flags
? {
iconName: "disabled",
label: <FlagsNote browser={browser} supportItem={item} />,
}
: null,
item.notes
? (Array.isArray(item.notes) ? item.notes : [item.notes]).map(
(note) => ({ iconName: "footnote", label: note })
)
: null,
versionIsPreview(item.version_added, browser)
? {
iconName: "footnote",
label: "Preview browser support",
}
: null,
// If we encounter nothing else than the required `version_added` and
// `release_date` properties, assume full support.
// EDIT 1-5-21: if item.version_added doesn't exist, assume no support.
isFullySupportedWithoutLimitation(item) &&
!versionIsPreview(item.version_added, browser)
? {
iconName: "footnote",
label: "Full support",
}
: isNotSupportedAtAll(item)
? {
iconName: "footnote",
label: "No support",
}
: null,
]
.flat()
.filter(isTruthy);
const hasNotes = supportNotes.length > 0;
return (
(i === 0 || hasNotes) && (
<React.Fragment key={i}>
<div className="bc-notes-wrapper">
<dt
className={`bc-supports-${getSupportClassName(
item,
browser
)} bc-supports`}
>
<CellText support={item} browser={browser} timeline={true} />
</dt>
{supportNotes.map(({ iconName, label }, i) => {
return (
<dd className="bc-supports-dd" key={i}>
<Icon name={iconName} />{" "}
{typeof label === "string" ? (
<span dangerouslySetInnerHTML={{ __html: label }} />
) : (
label
)}
</dd>
);
})}
{!hasNotes && <dd />}
</div>
</React.Fragment>
)
);
})
.filter(isTruthy);
}
}
function CompatCell({
browserId,
browserInfo,
support,
showNotes,
onToggle,
locale,
}: {
browserId: BCD.BrowserName;
browserInfo: BCD.BrowserStatement;
support: BCD.SupportStatement | undefined;
showNotes: boolean;
onToggle: () => void;
locale: string;
}) {
const supportClassName = getSupportClassName(support, browserInfo);
// NOTE: 1-5-21, I've forced hasNotes to return true, in order to
// make the details view open all the time.
// Whenever the support statement is complex (array with more than one entry)
// or if a single entry is complex (prefix, notes, etc.),
// we need to render support details in `bc-history`
// const hasNotes =
// support &&
// (asList(support).length > 1 ||
// asList(support).some(
// (item) =>
// item.prefix || item.notes || item.alternative_name || item.flags
// ));
const notes = getNotes(browserInfo, support!);
const content = (
<>
<CellText {...{ support }} browser={browserInfo} />
{showNotes && (
<dl className="bc-notes-list bc-history bc-history-mobile">{notes}</dl>
)}
</>
);
return (
<>
<td
className={`bc-support bc-browser-${browserId} bc-supports-${supportClassName} ${
notes ? "bc-has-history" : ""
}`}
aria-expanded={showNotes ? "true" : "false"}
onClick={notes ? () => onToggle() : undefined}
>
<button type="button" disabled={!notes} title="Toggle history">
{content}
<span className="offscreen">Toggle history</span>
</button>
</td>
</>
);
}
export const FeatureRow = React.memo(
({
index,
feature,
browsers,
activeCell,
onToggleCell,
locale,
}: {
index: number;
feature: {
name: string;
compat: CompatStatementExtended;
depth: number;
};
browsers: BCD.BrowserName[];
activeCell: number | null;
onToggleCell: ([row, column]: [number, number]) => void;
locale: string;
}) => {
const browserInfo = useContext(BrowserInfoContext);
if (!browserInfo) {
throw new Error("Missing browser info");
}
const { name, compat, depth } = feature;
const title = compat.description ? (
<span dangerouslySetInnerHTML={{ __html: compat.description }} />
) : (
<code>{name}</code>
);
const activeBrowser = activeCell !== null ? browsers[activeCell] : null;
let titleNode: string | React.ReactNode;
if (compat.bad_url && compat.mdn_url) {
titleNode = (
<div className="bc-table-row-header">
<abbr className="new" title={`${compat.mdn_url} does not exist`}>
{title}
</abbr>
{compat.status && <StatusIcons status={compat.status} />}
</div>
);
} else if (compat.mdn_url && depth > 0) {
titleNode = (
<a href={compat.mdn_url} className="bc-table-row-header">
{title}
{compat.status && <StatusIcons status={compat.status} />}
</a>
);
} else {
titleNode = (
<div className="bc-table-row-header">
{title}
{compat.status && <StatusIcons status={compat.status} />}
</div>
);
}
return (
<>
<tr>
<th className={`bc-feature bc-feature-depth-${depth}`} scope="row">
{titleNode}
</th>
{browsers.map((browser, i) => (
<CompatCell
key={browser}
browserId={browser}
browserInfo={browserInfo[browser]}
support={compat.support[browser]}
showNotes={activeCell === i}
onToggle={() => onToggleCell([index, i])}
locale={locale}
/>
))}
</tr>
{activeBrowser && (
<tr className="bc-history bc-history-desktop">
<td colSpan={browsers.length + 1}>
<dl className="bc-notes-list">
{getNotes(
browserInfo[activeBrowser],
compat.support[activeBrowser]!
)}
</dl>
</td>
</tr>
)}
</>
);
}
);