Flash on value change. Perfect for financial applications.
9 |
10 |
11 |
12 | `react-value-flash` will display a flashed value on screen based on some value change. This pattern is extremely common in financial applications, and at Lab49, we're focused on the finance industry.
13 |
14 | Incorporate this component into your application and pass along a number. As that number changes, this component will briefly flash a color, letting the user know the number has changed. By default, this component will flash green when the value changes up, or red when the value changes down.
15 |
16 | Not only are these colors configurable, but the properties of the flash itself and the formatting of the value are configurable as well.
17 |
18 | Furthermore, this component doesn't come with any styles, but does provide plenty of hooks to add your own styles. Even though flash color and transition properties are configurable as props, you can still use the generated classnames (which are also configurable) to add your own unique styles.
19 |
20 |
21 |
22 |
23 |
24 | This component is perfect for:
25 |
26 | - Trading platforms
27 | - Analytics dashboards
28 | - Monitoring dashboards
29 |
30 | ## Features
31 |
32 | - Written in TypeScript
33 | - Small, simple, configurable, performant
34 | - Maintained by a team of finance industry professionals
35 |
36 | ## Table of contents
37 |
38 | - [Demo](#demo)
39 | - [Installation](#installation)
40 | - [Usage](#usage)
41 | - [API](#api)
42 | - [`Flash`](#Flash)
43 | - [`Props`](#Props)
44 | - [`FlashDirection`](#FlashDirection)
45 | - [License](#License)
46 | - [TODO](#TODO)
47 |
48 | ## Demo
49 |
50 | Hosted demo: [https://master--5f3fca6e6b5eba0022c71e4e.chromatic.com/](https://master--5f3fca6e6b5eba0022c71e4e.chromatic.com/)
51 |
52 | You can also run the demo locally. To get started:
53 |
54 | ```sh
55 | git clone git@github.com:lab49/react-value-flash.git
56 | npm install
57 | npm run storybook
58 | ```
59 |
60 | ###### [⇡ Top](#table-of-contents)
61 |
62 | ## Installation
63 |
64 | ```sh
65 | npm install @lab49/react-value-flash
66 | ```
67 |
68 | ###### [⇡ Top](#table-of-contents)
69 |
70 | ## Usage
71 |
72 | ```js
73 | import { Flash } from '@lab49/react-value-flash';
74 |
75 |
76 | ```
77 |
78 | As discussed above, there are a number of classnames you can use to add your own styles. There is an example of doing exactly that in the include [Storybook](./stories/Flash.stories.tsx), but as an example, here's a description of the available classnames:
79 |
80 | | Class | Description |
81 | | --- | --- |
82 | | `.rvf_Flash` | Root DOM node |
83 | | `.rvf_Flash__value` | Rendered value, direct (and only) child of the root node. |
84 | | `.rvf_Flash--flashing` | Applied only when the component is in the flashing state. |
85 | | `.rvf_Flash--flashing-up` | Applied when flashing 'up'. |
86 | | `.rvf_Flash--flashing-down` | Applied when flashing 'down'. |
87 | | `.rvf_Flash--positive` | Applied when the value is positive. |
88 | | `.rvf_Flash--negative` | Applied when the value is negative. |
89 |
90 | ###### [⇡ Top](#table-of-contents)
91 |
92 | ## API
93 |
94 | ### `Flash`
95 |
96 | `` is a `(props: Props) => JSX.Element`. See `Props` below for a description of the avilable props.
97 |
98 | ```ts
99 | import { Flash } from '@lab49/react-value-flash';
100 |
101 | const MyComponent = () => ;
102 | ```
103 |
104 | ### `Props`
105 |
106 | ```ts
107 | interface Props {
108 | /**
109 | * Color value when the component flashes 'down'.
110 | */
111 | downColor?: string;
112 | /**
113 | * One of the built in formatters.
114 | */
115 | formatter?: 'currency' | 'percentage' | 'number';
116 | /**
117 | * Pass your own formatter function.
118 | */
119 | formatterFn?: Formatter;
120 | /**
121 | * Prefix for the CSS selectors in the DOM.
122 | */
123 | stylePrefix?: string;
124 | /**
125 | * Amount of time the flashed state is visible for, in milliseconds.
126 | */
127 | timeout?: number;
128 | /**
129 | * Custom CSS transition property.
130 | */
131 | transition?: string;
132 | /**
133 | * Transition length, in milliseconds.
134 | */
135 | transitionLength?: number;
136 | /**
137 | * Color value when the component flashes 'up'.
138 | */
139 | upColor?: string;
140 | /**
141 | * Value to display. The only required prop.
142 | */
143 | value: number;
144 | }
145 | ```
146 |
147 | ### `FlashDirection`
148 |
149 | ```ts
150 | enum FlashDirection {
151 | Down = 'down',
152 | Up = 'up',
153 | }
154 | ```
155 |
156 | ###### [⇡ Top](#table-of-contents)
157 |
158 | ## License
159 |
160 | MIT @ [Lab49](https://lab49.com)
161 |
162 | ###### [⇡ Top](#table-of-contents)
163 |
164 | ## Sponsored by Lab49
165 |
166 |
167 |
168 |
169 |
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | /* Visit https://aka.ms/tsconfig.json to read more about this file */
4 |
5 | /* Basic Options */
6 | // "incremental": true, /* Enable incremental compilation */
7 | "target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
8 | "module": "esnext", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
9 | "lib": ["dom", "esnext"], /* Specify library files to be included in the compilation. */
10 | // "allowJs": true, /* Allow javascript files to be compiled. */
11 | // "checkJs": true, /* Report errors in .js files. */
12 | "jsx": "react", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
13 | // "declaration": true, /* Generates corresponding '.d.ts' file. */
14 | // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
15 | // "sourceMap": true, /* Generates corresponding '.map' file. */
16 | // "outFile": "./", /* Concatenate and emit output to single file. */
17 | "outDir": "./dist", /* Redirect output structure to the directory. */
18 | // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
19 | // "composite": true, /* Enable project compilation */
20 | // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
21 | // "removeComments": true, /* Do not emit comments to output. */
22 | // "noEmit": true, /* Do not emit outputs. */
23 | // "importHelpers": true, /* Import emit helpers from 'tslib'. */
24 | // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
25 | // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
26 |
27 | /* Strict Type-Checking Options */
28 | "strict": true, /* Enable all strict type-checking options. */
29 | // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
30 | // "strictNullChecks": true, /* Enable strict null checks. */
31 | // "strictFunctionTypes": true, /* Enable strict checking of function types. */
32 | // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
33 | // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
34 | // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
35 | // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
36 |
37 | /* Additional Checks */
38 | // "noUnusedLocals": true, /* Report errors on unused locals. */
39 | // "noUnusedParameters": true, /* Report errors on unused parameters. */
40 | // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
41 | // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
42 |
43 | /* Module Resolution Options */
44 | "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
45 | "resolveJsonModule": true,
46 | // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
47 | // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
48 | // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
49 | // "typeRoots": [], /* List of folders to include type definitions from. */
50 | // "types": [], /* Type declaration files to be included in compilation. */
51 | // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
52 | "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
53 | // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
54 | // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
55 |
56 | /* Source Map Options */
57 | // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
58 | // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
59 | // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
60 | // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
61 |
62 | /* Experimental Options */
63 | // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
64 | // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
65 |
66 | /* Advanced Options */
67 | "skipLibCheck": true, /* Skip type checking of declaration files. */
68 | "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
69 | },
70 | "include": [
71 | "src",
72 | "stories",
73 | "."
74 | ]
75 | }
76 |
--------------------------------------------------------------------------------
/stories/Flash.stories.tsx:
--------------------------------------------------------------------------------
1 | import React from "react";
2 | import { Story, ComponentMeta, ComponentStory } from "@storybook/react";
3 | import { userEvent, within } from "@storybook/testing-library";
4 | import { expect } from "@storybook/jest";
5 |
6 | import { Flash, Props } from "../src/Flash";
7 | import { useInterval } from "./useInterval";
8 | import { ValueSetter } from "./components/ValueSetter";
9 | import pkg from "../package.json";
10 |
11 | export default {
12 | title: "Flash",
13 | component: Flash,
14 | parameters: {
15 | componentSubtitle: pkg.description,
16 | },
17 | } as ComponentMeta;
18 |
19 | const numberMap: { [key: string]: string } = {
20 | 0: "zero",
21 | 1: "one",
22 | 2: "two",
23 | 3: "three",
24 | 4: "four",
25 | 5: "five",
26 | 6: "six",
27 | 7: "seven",
28 | 8: "eight",
29 | 9: "nine",
30 | };
31 |
32 | const Template: Story = (args) => {
33 | return (
34 |
35 | {(value: number) => }
36 |
37 | );
38 | };
39 |
40 | export const Default = Template.bind({});
41 |
42 | export const StreamingData = () => {
43 | const [hasRan, setHasRan] = React.useState(false);
44 | const [val, setVal] = React.useState(1);
45 |
46 | useInterval(() => {
47 | if (Math.random() > 0.8) {
48 | setHasRan(true);
49 | setVal(Math.floor(Math.random() * 100) - 50);
50 | }
51 | }, 200);
52 |
53 | return (
54 |