Giter VIP home page Giter VIP logo

react-native-background-readable-color's Introduction

@radicalcondor/react-native-background-readable-color

A React Native package that provides:

  • A component that takes a backgroundColor and returns a readable color through a render prop
  • A text component that takes a backgroundColor and changes its color to a readable color
  • A hook that returns a readable color given another color
  • Memoized versions of the above
  • Mechanism for getting/setting preset colors

Background

I wanted a text component that was background color aware. Initially, I thought I could get a reference to the parent component and retrieve the background color, but I couldn't find a way to reliably achieve that. So I came up with the next best thing I could think of, a wrapper component that receives a backgroundColor and renders any text inside it with a readable color.

Installation

yarn add @radicalcondor/react-native-background-readable-color

Example

You can run the example project by doing

yarn

It will exit 1 here, but that's okay. and then

cd example
yarn
yarn pods

then

yarn ios

or

yarn android

Usage

useBackgroundReadableColor / useBackgroundReadableColorMemo hook

    backgroundColor?: string;
    presetName?: string;
import { Text } from 'react-native' 
import { useBackgroundReadableColorMemo } from "@radicalcondor/react-native-background-readable-color";

const props = {
  backgroundColor: '#000',
  presetName: undefined,
};

const color = useBackgroundReadableColorMemo(props);

BackgroundReadableColorText component

    backgroundColor?: string;
    children: string;
    presetName?: string;
    render: any;
    useMemo?: boolean;
import { Text } from 'react-native' 
import BackgroundReadableColorText from "@radicalcondor/react-native-background-readable-color";

const bgColor = 'black';

<BackgroundReadableColorText backgroundColor={bgColor} render={Text}>
    A readable color text in white
</BackgroundReadableColorText>

BackgroundReadableColor component

    backgroundColor?: string;
    presetName?: string;
    render: (color?: string) => React.ReactNode;
    useMemo?: boolean;
import { Text } from 'react-native' 
import { BackgroundReadableColor } from "@radicalcondor/react-native-background-readable-color";

const bgColor = 'white';

<ReadableBackgroundColor
    backgroundColor={bgColor}
    render={(color?: string) => <Text style={{color: color || 'cyan'}}>A readable color text in black</Text>}
/>

using ReadablePresets

You can create presets for your readable colors, it takes a name and an object with a dark color, that will try to be used on a dark background and a light color for light backgrounds. ReadablePresets is a singleton and the configuration can be done anywhere in your project.

    dark?: string;
    light?: string;
import { Text } from 'react-native' 
import BackgroundReadableColorText, { ReadablePresets } from "@radicalcondor/react-native-background-readable-color";

ReadablePresets.setPreset('RED', {
    dark: '#ff9c9c', // color to be used if its a dark background
    light: '#6d0000', // color to be used if its a light background
});

const bgColor = 'black';

<BackgroundReadableColorText backgroundColor={bgColor} render={Text}>
    A readable color text in #ff9c9c
</BackgroundReadableColorText>

This package uses readableColor function from polished lib to compute the colors. If you would like to read more check here.

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT

react-native-background-readable-color's People

Contributors

cainaleaouk avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.