-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathindex.tsx
79 lines (68 loc) · 1.63 KB
/
index.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
import * as React from 'react';
const DEFAULT_CDN_URL =
'https://cdn.jsdelivr.net/gh/lipis/flag-icons/flags/4x3/';
const DEFAULT_CDN_SUFFIX = 'svg';
// offset between uppercase ascii and regional indicator symbols
const OFFSET = 127397;
interface EmojiProps extends React.HTMLAttributes<HTMLSpanElement> {
cdnSuffix?: string;
cdnUrl?: string;
countryCode: string;
style?: React.CSSProperties;
svg?: false;
}
interface ImgProps extends React.ImgHTMLAttributes<HTMLImageElement> {
cdnSuffix?: string;
cdnUrl?: string;
countryCode: string;
style?: React.CSSProperties;
svg?: true;
}
export type ReactCountryFlagProps = EmojiProps | ImgProps;
export const ReactCountryFlag = ({
cdnSuffix = DEFAULT_CDN_SUFFIX,
cdnUrl = DEFAULT_CDN_URL,
countryCode,
style,
svg = false,
...props
}: ReactCountryFlagProps) => {
if (typeof countryCode !== 'string') {
return null;
}
if (svg) {
const flagUrl = `${cdnUrl}${countryCode.toLowerCase()}.${cdnSuffix}`;
return (
<img
{...props}
src={flagUrl}
style={{
display: 'inline-block',
width: '1em',
height: '1em',
verticalAlign: 'middle',
...style,
}}
/>
);
}
const emoji = countryCode
.toUpperCase()
.replace(/./g, char => String.fromCodePoint(char.charCodeAt(0) + OFFSET));
return (
<span
role="img"
{...props}
style={{
display: 'inline-block',
fontSize: '1em',
lineHeight: '1em',
verticalAlign: 'middle',
...style,
}}
>
{emoji}
</span>
);
};
export default ReactCountryFlag;