Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-flag-icon-css

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-flag-icon-css

React SVG country flags component

  • 1.0.19
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6.9K
decreased by-12.99%
Maintainers
1
Weekly downloads
 
Created
Source

A simple React SVG country flags component: it works with Css Modules (default) or standard Css.

NPM version NPM downloads license Build Status Greenkeeper badge dependencies Status devDependencies Status peerDependencies Status codecov Coverage Status

Installation

React Flag Icon Css is distributed as an npm package.

We recommend installing and managing npm packages with yarn or npm 5:

$ yarn add react-flag-icon-css

or with npm 1:

$ npm install --save react-flag-icon-css

1 You can omit --save if using npm 5.

Using in a create-react-app app

Apps bootstrapped with create-react-app support this module out of the box, remember to set useCssModules to false (unfortunately create-react-app does not currently support Css modules).

import FlagIconFactory from 'react-flag-icon-css'

const FlagIcon = FlagIconFactory(React, { useCssModules: false })
// ...

Prerequisites for custom apps

We recommend using the webpack 2 module bundler and ecosystem to assemble your app, but this module also works with webpack 1 and should work with other bundlers.

If you are using webpack, you will need to install and configure a few commmonly used modules (see the webpack 2 example project):

$ yarn add -D babel-loader css-loader file-loader style-loader extract-text-webpack-plugin

or with npm:

$ npm install --save-dev ...

Basic usage

Import FlagIconFactory from react-flag-icon-css, it accepts the React module as the first argument and creates the FlagIcon component. This approach ensures that FlagIcon uses your app's React instance, avoiding issues such as two versions of React being loaded at the same time.

import React from 'react'
import ReactDOM from 'react-dom'
import FlagIconFactory from 'react-flag-icon-css'

const FlagIcon = FlagIconFactory(React)
// If you are not using css modules, write the following:
// const FlagIcon = FlagIconFactory(React, { useCssModules: false })

const App = (props = {}) =>
  <div>
    <FlagIcon code={props.code} size={props.size} />
  </div>

const rootEL = document.body.querySelector('#app')

const appProps = { code: 'it', size: '3x' }
ReactDOM.render(<App {...appProps} />, rootEL)

A webpack 2 example project is available.

:flags: FlagIcon props

The entries marked with &ast; are required.

PropTypeFlow TypeDefaultDescriptionSupported values
code *StringFlagIconCodeType 1ISO 3166-1-alpha-2 code.The list is here.
sizeStringFlagIconSizeTypelg, 2x, 3x, 4x, 5x
flipStringFlagIconFlipTypehorizontal, vertical
rotateNumberFlagIconRotateType30, 60, 90, 180, 270
squaredBooleanbooleanfalseUses the 1x1 image if true.
ComponentStringstringspane.g span, div
classNameStringstringThis is always appended as-is to class in the HTML.e.g some-class
styleNameStringstringThis is mapped to a CSS module and appended to class in
the HTML.
e.g some-class
ChildrenStringReact$Element<*>React element.e.g <Something />

Remember to always build FlagIcon with FlagIconFactory.

1 Upgrade to version 1.0.17 or later of this module.

:factory: FlagIconFactory

The entries marked with &ast; are required.

ArgumentTypeFlow TypeDescriptionSupported values
React *ModuleReactModuleYour app's React instance.Versions in peerDependencies.
optionsObjectFlagIconOptionsType

:factory: FlagIconFactory options

ArgumentTypeFlow TypeDescriptionSupported valuesDefault
useCssModulesBooleanBooleanUse CSS modules styles (your module bundler must be correctly setup).true, falsetrue
customCodes 2ObjectObjectAn object literal whose keys are your custom codes.
Example.
themeStylesObjectCssModuleSet this if useCssModules is true and a) you want to apply styles to FlagIcon
using .theme-base and/or
b) you are using custom flags.

2 Upgrade to version 1.0.17 or later of this module.

Configuration for Facebook's Flow

You can skip this section if you are not using Flow in your project.

The following are the rules that you need in your .flowconfig:

[options]
...
# Used in tests.
suppress_comment=\\(.\\|\n\\)*\\$FlowExpectError
suppress_comment=\\(.\\|\n\\)*\\$FlowFixMe
# SEE: https://github.com/facebook/flow/issues/1895
suppress_type=$FlowIssue
...
[ignore]
...
# SEE: https://github.com/stylelint/stylelint/issues/2322
.*/node_modules/stylelint/.*
...

Custom flags

Required parameters

  • Always set FlagIconFactory options.customCodes to make this module aware of your codes. Otherwise: runtime warnings in development (and Flow errors, if you use it).
  • If using Css Modules, import your styles in someVariable and set FlagIconFactory options.themeStyles to someVariable. Otherwise: runtime Css Modules errors.
  • Else if using standard Css, make sure to import the styles. Otherwise: the custom images won't be loaded.
  • If using Flow use CustomFlagIconFactory and not FlagIconFactory. Otherwise: Flow errors.

Quick example

We recommend organizing your custom flags in a folder similar to example-custom-flags. You may copy-paste it in the root of your app and replace the codes and images.

Example folder structure:

|-- app.js
|-- example-custom-flags
    |--index.js
    |--istyles.css
    |--images
       |--1x1
          |--ex1.svg
          |--ex2.svg
          |--ex3.svg
       |--4x3
          |--ex1.svg
          |--ex2.svg
          |--ex3.svg

Write the styles for each one of your codes, and load the appropriate images:

/* example-custom-flags/styles.css */
/**
 * Note: you can use PostCSS, SASS or whatever preprocessor your
 * app is using here.
 */
.flag-icon-ex1 {
  background-image: url(../images/4x3/ex1.svg);
}

.flag-icon-ex1.flag-icon-squared {
  background-image: url(../images/1x1/ex1.svg);
}

/* ... */

Import the styles and export them and the object with your codes:

/* example-custom-flags/index.js */
import styles from './styles.css'

const codes = {
  ex1: 'Example 1 country',
  ex2: 'Example 2 country',
  ex3: 'Example 3 country',
}

// You can comment or remove the following line if you don't use Facebook's Flow.
export type CustomCodeType = $Keys<typeof codes>

export { styles, codes }

Import CustomFlagIconFactory in your app and build FlagIcon as shown:

/* app.js */
import React from 'react'
import { CustomFlagIconFactory } from 'react-flag-icon-css'
import { styles, codes } from './my-custom-flags'

// Using 'react-css-modules'? Use this:
const optionsCssModules = { customCodes: codes, themeStyles: styles }
const FlagIconCssModules = CustomFlagIconFactory(React, optionsCssModules)

// Using global CSS? Use this instead:
const options = { useCssModules: false, customCodes: codes }
const FlagIcon = CustomFlagIconFactory(React, options)

Note: when you build FlagIcon with CustomFlagIconFactory, it can be used with both built-in and custom codes.

Development

Runtime type checking: in development mode (process.env.NODE_ENV !== 'production'), when using unsupported props or prop values, you are warned at runtime by Failed prop type errors in the browser console. Feature powered by prop-types.

Static type checking: if you use Facebook's flow in your app (otherwise you can skip this section, unless you want to submit a pull request), it should automatically pick up this package's definitions from the .js.flow files that are distributed with it, checking your code accordingly when you run yarn flow. Using the latest Flow version or the version in ./package.json is recommended. We also recommend using a Flow-aware editor such as Nuclide for Atom.

Contributing

Contributions are welcome:

  • :pencil2: Write code: use a topic branch, follow the AngularJS commit style guidelines and check that yarn prepublish (build, test, type checking, linting ...) returns zero errors before opening a PR. If you want to make major modifications to the code, please open an issue to discuss them first.

  • :bug: Report a bug: first, search all issues. If nothing turns up, open a new issue and be as specific as possible, so that we can reproduce your setup and find out if it is a bug or a configuration issue.

  • :triangular_ruler: Propose a feature: first, search all issues. If nothing turns up, open a new issue and describe what the proposed feature should do, why you think it is important and an example use case.

Thanks! :blue_heart:

Find this module useful?

:star: Starring it lets you keep track of this project and helps more people discover it.

Source of the flags

This project uses the great SVG country flags from flag-icon-css.

License

This project is licensed under the terms of the MIT license.

Keywords

FAQs

Package last updated on 02 Jul 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc