• Stars
    star
    106
  • Rank 325,871 (Top 7 %)
  • Language
    JavaScript
  • License
    Apache License 2.0
  • Created over 3 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Generate platform native colors for iOS, Android & Web

platform-colors Logo

platform-colors

Generate platform native colors for iOS, Android & Web

Build Status NPM Package License Developed at Klarna

Using colors from the underlying platform primitives is powerful, but maintaining it when targeting multiple platforms is quite cumbersome. With this CLI you can generate colors and entrypoint for both iOS, Android, and Web with ease.

Under the hood we are using PlatformColor on React Native and CSS custom properties on web.

Features

Easy to use

Type/JavaScript constants are generated automatically – using it with React/Native is as simple as importing the color name.

Fast

Using platform colors enables you to change from light/dark mode instantly and without any additional rerenders.

Accessible

By utilizing the underlying platforms high contrast colors are supported out of the box.

Usage

npx @klarna/platform-colors

The first time you run the command it will prompt you which platforms you want to generate files for which will create a file with the following format:

// platform-colors.config.js
module.exports = {
  colors: {
    background: {
      light: '#ffffff',
      dark: '#000000',
    },
    accent: 'pink',
  },
  javascript: {
    typescript: true,
    outputDirectory: 'src/colors/',
  },
  ios: {
    outputDirectory: 'ios/YourApp/Images.xcassets/',
  },
  android: {
    outputDirectory: 'android/app/src/main/res/',
  },
  css: {
    outputDirectory: 'static/css/',
  },
};

NOTE: You need to re-run the command after each change to the config to update the generated files.

Now go ahead and inspect your android, ios and web folders. You should have your color definitions on each platform.

Configuration

colors

An object where the key is the color name, and the value is either a string or an object containing light and optionally highContrastLight, dark & highContrastDark properties.

Example:

{
  colors: {
    contrasted: {
      light: '#ccc',
      highContrastLight: '#fff',
      dark: '#333',
      highContrastDark: '#000',
    }
  }
}

ios

An object containing outputDirectory which should be an .xcassets directory.

Example:

{
  ios: {
    outputDirectory: 'ios/YourProject/Assets.xcassets/'
  }
}

android

An object containing outputDirectory which should be an Android res directory.

Example:

{
  android: {
    outputDirectory: 'android/app/src/main/res/'
  }
}

css

An object containing outputDirectory and filename which should be a directory where you store CSS files and if you want to change the default filename from colors.css.

Example:

{
  css: {
    filename: 'example.css',
    outputDirectory: 'static/css/'
  }
}

javascript

An object containing outputDirectory which should be a directory where you store your Type/JavaScript files and typescript which is set to true if you want the output in TypeScript.

Example:

{
  "javascript": {
    "typescript": true,
    "outputDirectory": "src/colors/"
  }
}

Getting hex value from dynamic color

Note: You must first make sure you've added @klarna/platform-colors as a dependency and recompiled the app.

import { PlatformColor } from 'react-native';
import { resolveColorSync } from '@klarna/platform-colors';

const hexColor = resolveColorSync(PlatformColor('colorName'));

prefix

We prefix all colors with rnpc_ by default, you can override that with this option.

Example:

{
  prefix: 'custom_',
  // colors...
}

Development Setup

Install dependencies and make sure the tests are working

yarn install
yarn test

Example App

There's an example React Native App available to test under the examples app.

cd examples/ColorViewerApp
yarn
pod install --project-directory=ios

Running it either on ios or android by:

yarn ios

or

yarn android

Screenshots

Screenshot in dark mode Screenshot in dark mode

How to contribute

See our changelog.

Deployment

Update version in package.json and merge to master. This will publish the package to NPM, create a draft release on GitHub and a version tag. Edit the release with additional information and publish it.

Copyright © 2021 Klarna Bank AB

For license details, see the LICENSE file in the root of this project.

More Repositories

1

gram

Gram is Klarna's own threat model diagramming tool
TypeScript
230
star
2

webtraversallibrary

The Web Traversal Library (WTL) is a Python library for abstracting web interactions on top of a base execution layer such as Selenium.
HTML
67
star
3

qrcode-renderer

QR Code renderer is a dependency-free library to render QR Codes. The library makes it simple to integrate with any UI framework and comes with a prebuilt SVG renderer for the web.
TypeScript
64
star
4

glass

A semantic search tool for Erlang that supports large code-bases.
Erlang
48
star
5

flink-connector-dynamodb

Apache Flink connector sink for AWS DynamoDB
Java
33
star
6

kflow

Erlang
32
star
7

kache

A generic, in-memory caching application based on ETS.
Erlang
32
star
8

kappa

Erlang
23
star
9

mediamux

A utility for writing responsive React components in a concise, maintainable, mobile-first way.
TypeScript
21
star
10

heapview

node heap dump visualizer
Rust
21
star
11

react-native-vector-drawable

Android vector drawables in React Native
Java
19
star
12

mleko

Simplify and accelerate your machine learning development with mleko. Designed with modularity and customization in mind, it seamlessly integrates into your existing workflows. Its robust caching system optimizes performance, taking you from data ingestion to finalized models with unparalleled efficiency.
Python
11
star
13

moomin

TypeScript
10
star
14

postgres-to-docs

The smoooth way to document your postgres database
TypeScript
5
star
15

incubator-popularity-graph

JavaScript
5
star
16

service_communication_map

Java
5
star
17

fastlane-plugin-secrets_manager_storage

A Fastlane plugin to enable securely storing your app's signing keys in AWS Secrets Manager
Ruby
5
star
18

flink-connector-jdbc-1.8

Java library provides Apache Flink connector sink for JDBC database that can be used with Flink 1.8 runtime version.
Java
5
star
19

iterator-erl

Erlang lazy sequences simulating stdlib lists module API
Erlang
4
star
20

nest-lambda-microservice

Custom transporter implementation for running NestJS based applications on AWS Lambda.
TypeScript
1
star
21

test-cla

1
star