react-flagpack

repository·main·Indexed 41 days ago

https://github.com/yummygum/react-flagpack

A React library providing over 250 flag icons with a highly configurable Flag component. Supports various sizes (S, M, L), gradients (top-down, real-linear, real-circular), borders, and drop shadows. Includes a CLI to automate the injection of SVG assets into project static or public folders for frameworks like NextJS, Gatsby, and Remix. Version 2.0.6.

Tokens
2.8K
Snippets
13
Records
17
Agent score
51%

What's inside react-flagpack

  1. Install react-flagpack

    main

    Install the package via npm. Note that react-flagpack requires files to be injected into your project's static/public folder. The package includes a CLI to automate this, but you must configure your package.json to execute it.

    To ensure the assets are correctly placed, add a postinstall script to your package.json. If your package manager does not run postinstall automatically, you may need to run the command manually.

    For Gatsby projects, you must pass the --framework gatsby flag to the CLI. For other frameworks like NextJS, create-react-app, or Remix, the default configuration is sufficient.

    $ npm install react-flagpack --save
    {
      "scripts": {
        "postinstall": "react-flagpack"
      },
      "dependencies": {
        "react-flagpack": "^2.0.5"
      }
    }
  2. Manage the Create React App test application via npm scripts

    main

    The create-react-app test application in this repository provides several standard npm scripts for development, testing, building, and configuration management.

    • Development: Use npm start to run the app in development mode. The app will be available at http://localhost:3000 and will automatically reload on changes.
    • Testing: Use npm test to launch the test runner in interactive watch mode.
    • Production Build: Use npm run build to create a minified, optimized production build in the build folder. This version is ready for deployment.
    • Configuration Customization: Use npm run eject if you need full control over the underlying build tools (webpack, Babel, ESLint, etc.).

    Warning: npm run eject is a one-way operation. Once executed, you cannot revert to the managed configuration.

    npm start
    npm test
    npm run build
    npm run eject
  3. Migrate to react-flagpack v2.0.0

    main

    When upgrading to version 2.0.0 or higher, follow these steps:

    1. Add post-install hook: Add "postinstall": "react-flagpack" to your package.json scripts.
    2. Install: Run your install command (e.g., yarn install) ensuring you are on at least react-flagpack@2.0.2.
    3. Verify Assets: After installation, a flags folder should appear in your project's public or static folder.
    4. Import Styles: Styles are no longer injected automatically. You must manually import the CSS to enable styling props and server-side support. You can do this in a component:
      import 'react-flagpack/dist/style.css'
      Or in a global CSS file:
      @import 'react-flagpack/dist/style.css';
  4. Flag asset naming conventions

    main

    When using the generateFlags.ts utility, flag files are renamed from their base filename to one of the following country identifiers. This enables flexible asset selection based on the identifier type you prefer to use in your code.

    Available Identifiers:

    • alpha2: The two-letter country code.
    • alpha3: The three-letter country code.
    • numeric: The numeric country code.

    Example Mapping: If a source file is us.svg, the script will generate files like us.svg (alpha2), usa.svg (alpha3), and potentially a numeric version if available in the countryCodeList.json mapping.

  5. Generate flag assets via generateFlags.ts

    main

    The generateFlags.ts script is a utility used to process and distribute flag SVG assets. It reads country data from flagpack-core/countryCodeList.json and copies SVG files from node_modules/flagpack-core/svg into a local ./dist/flags directory.

    For each flag, the script generates multiple files based on the country's identifiers (alpha2, alpha3, and numeric codes). This allows you to reference flags using different naming conventions in your application.

    Workflow:

    1. Creates a ./dist/flags directory with subdirectories for each size: s, m, and l.
    2. Iterates through the source SVG files in node_modules/flagpack-core/svg/.
    3. Maps each file to its corresponding country identifiers.
    4. Copies the SVG files to the destination, renaming them to use the country's alpha2, alpha3, or numeric code as the filename.
    // The script performs the following directory structure creation:
    // ./dist/flags/s/
    // ./dist/flags/m/
    // ./dist/flags/l/
  6. Use the Flag component in JSX

    main

    To use flags in your React application, import the Flag component and its required CSS file.

    Note: Importing the CSS is mandatory if you want to use props other than code and size (such as gradient, hasDropShadow, etc.). You can import the CSS directly in a component or via an @import in a global CSS file.

    import Flag from 'react-flagpack'
    import 'react-flagpack/dist/style.css'
    
    // Usage
    <Flag
      code="NL"
      gradient="real-linear"
      size="m"
      hasDropShadow
    />
    import Flag from 'react-flagpack'
    import 'react-flagpack/dist/style.css'
    
    <Flag
      code="NL"
      gradient="real-linear"
      size="m"
      hasDropShadow
    />