|
1 | | -import React, { Component } from "react"; |
| 1 | +import React, { useState, useEffect } from "react"; |
2 | 2 | import PropTypes from "prop-types"; |
3 | 3 |
|
4 | | -const getUrl = (code) => { |
5 | | - try { |
6 | | - return require(`./img/flags/${code}.png`); |
7 | | - } catch (error) { |
8 | | - return require("./img/flags/none.png"); |
9 | | - } |
10 | | -}; |
11 | | - |
12 | | -const getStyle = (code, isGeneric) => ({ |
13 | | - backgroundColor: isGeneric ? "#78BEDB" : "transparent", |
| 4 | +const getStyle = (url) => ({ |
| 5 | + position: "relative", |
| 6 | + display: "inline-flex", |
| 7 | + alignItems: "center", |
| 8 | + justifyContent: "center", |
| 9 | + width: "27px", |
| 10 | + height: "18px", |
| 11 | + marginRight: "5px", |
| 12 | + backgroundColor: "transparent", |
| 13 | + backgroundImage: url ? `url(${url})` : "none", |
14 | 14 | backgroundRepeat: "no-repeat", |
15 | | - backgroundImage: isGeneric ? "none" : `url(${getUrl(code)})`, |
16 | | - backgroundPositionY: "50%", |
17 | | - backgroundPositionX: "50%", |
| 15 | + backgroundPosition: "center", |
| 16 | + backgroundSize: "contain", |
18 | 17 | color: "#FFF", |
19 | | - textTransform: "uppercase", |
20 | | - display: "inline-block", |
21 | | - marginRight: "5px", |
22 | 18 | fontWeight: "bold", |
23 | | - width: "27px", |
24 | | - height: "18px", |
25 | | - lineHeight: "18px", |
26 | | - verticalAlign: "middle", |
27 | | - textAlign: "center" |
| 19 | + textTransform: "uppercase", |
28 | 20 | }); |
29 | 21 |
|
30 | | -class Flag extends Component { |
31 | | - constructor(props) { |
32 | | - super(props); |
33 | | - } |
| 22 | +const overlayStyle = { |
| 23 | + position: "absolute", |
| 24 | + left: 0, |
| 25 | + top: 0, |
| 26 | + width: "100%", |
| 27 | + height: "100%", |
| 28 | + display: "flex", |
| 29 | + alignItems: "center", |
| 30 | + justifyContent: "center", |
| 31 | + fontSize: "8px", |
| 32 | + fontWeight: "900", |
| 33 | + color: "#000", |
| 34 | + textTransform: "uppercase", |
| 35 | + whiteSpace: "nowrap", |
| 36 | + transform: "scale(0.9, 1.1)", |
| 37 | +}; |
| 38 | + |
| 39 | +function Flag({ culture = "", onClick, title }) { |
| 40 | + const [flagUrl, setFlagUrl] = useState(undefined); |
| 41 | + const [isFallback, setIsFallback] = useState(false); |
| 42 | + |
| 43 | + useEffect(() => { |
| 44 | + try { |
| 45 | + setFlagUrl(require(`./img/flags/${culture}.png`).default); |
| 46 | + setIsFallback(false); |
| 47 | + } catch { |
| 48 | + try { |
| 49 | + setFlagUrl(require("./img/flags/none.png").default); |
| 50 | + setIsFallback(true); |
| 51 | + } catch { |
| 52 | + setFlagUrl(undefined); |
| 53 | + setIsFallback(true); |
| 54 | + } |
| 55 | + } |
| 56 | + }, [culture]); |
34 | 57 |
|
35 | | - render() { |
36 | | - const isGeneric = !this.props.culture.includes("-"); |
37 | | - return <div |
38 | | - onClick={this.props.onClick} |
39 | | - title={this.props.title} |
40 | | - style={getStyle(this.props.culture, isGeneric)}>{isGeneric ? this.props.culture : ""}</div>; |
41 | | - } |
| 58 | + return ( |
| 59 | + <div onClick={onClick} title={title} style={getStyle(flagUrl)}> |
| 60 | + {isFallback && culture ? <div style={overlayStyle}>{culture}</div> : null} |
| 61 | + </div> |
| 62 | + ); |
42 | 63 | } |
43 | 64 |
|
44 | 65 | Flag.propTypes = { |
|
0 commit comments