forked from electricitymaps/electricitymaps-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds new Flag component (electricitymaps#50)
- Loading branch information
1 parent
a97c006
commit 22c59b9
Showing
4 changed files
with
43 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,45 @@ | ||
import { resolvePath } from 'react-router-dom'; | ||
import flags from 'country-flag-icons/react/3x2'; | ||
|
||
const DEFAULT_FLAG_SIZE = 48; | ||
const DEFAULT_FLAG_SIZE = 18; | ||
|
||
const getFlagFileName = (zoneId: string) => { | ||
// Return (zonesConfig[zoneId.toUpperCase()] || {}).flag_file_name; TODO: enable when config setup | ||
if (zoneId === 'DK-DK1') { | ||
return 'dk.png'; | ||
} | ||
|
||
if (zoneId === 'ES') { | ||
return 'es.png'; | ||
} | ||
const SPECIAL_ZONE_NAMES = { | ||
AUS: 'AU', | ||
} as { [index: string]: string }; | ||
|
||
return 'us.png'; | ||
}; | ||
function getCountryName(zoneId: string) { | ||
const country = zoneId.split('-')[0]; | ||
const flagName = SPECIAL_ZONE_NAMES[country] || country; | ||
return flagName.toUpperCase(); | ||
} | ||
|
||
const getFlagUri = function (zoneId: string, flagSize = DEFAULT_FLAG_SIZE) { | ||
if (!zoneId) { | ||
return; | ||
} | ||
const flagFile = getFlagFileName(zoneId); | ||
return resolvePath(`flags_iso/${flagSize}/${flagFile}`).pathname; | ||
}; | ||
type HTMLSVGElement = HTMLElement & SVGElement; | ||
interface CountryFlagProps | ||
extends React.HTMLAttributes<HTMLSVGElement>, | ||
React.SVGAttributes<HTMLSVGElement> { | ||
zoneId: string; | ||
size?: number; | ||
} | ||
|
||
export function CountryFlag({ | ||
zoneId, | ||
flagSize = DEFAULT_FLAG_SIZE, | ||
size = DEFAULT_FLAG_SIZE, | ||
...props | ||
}: { | ||
zoneId: string; | ||
flagSize?: number; | ||
className?: string; | ||
}) { | ||
const flagUri = getFlagUri(zoneId, flagSize); | ||
}: CountryFlagProps) { | ||
const countryName = getCountryName(zoneId) as keyof typeof flags; | ||
const FlagIcon = flags[countryName]; | ||
|
||
if (flagUri === undefined) { | ||
return <div>No flag</div>; | ||
if (!FlagIcon) { | ||
return <span className={`text-[14px]`}>🏴☠️</span>; | ||
} | ||
|
||
return <img src={flagUri} alt={zoneId} {...props} />; | ||
return ( | ||
<FlagIcon | ||
title="TODO" | ||
width={size} | ||
height={Math.floor((size / 3) * 2)} | ||
style={{ | ||
minWidth: size, | ||
}} | ||
{...props} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters