|
1 | | -# simplr-loaders |
2 | | -React css loaders with no prerequisite style loading. |
| 1 | +# simplr-loaders |
| 2 | +React CSS loaders with no prerequisites for required styles loading. |
| 3 | + |
| 4 | +# Get started |
| 5 | + |
| 6 | +```sh |
| 7 | +$ npm install simplr-loaders --save |
| 8 | +``` |
| 9 | + |
| 10 | +# Usage |
| 11 | + |
| 12 | +```ts |
| 13 | +import * as React from "react"; |
| 14 | +import { BubbleLoader } from "simplr-loaders"; |
| 15 | + |
| 16 | +export class MyComponent extends React.Component<{}, {}> { |
| 17 | + render() { |
| 18 | + return <BubbleLoader />; |
| 19 | + } |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +## Loaders |
| 24 | + |
| 25 | +This package currently contains two loaders: |
| 26 | +- `SpinnerLoader` |
| 27 | +- `BubbleLoader` |
| 28 | + |
| 29 | +If you'd like to see more loaders here feel free to contribute. |
| 30 | + |
| 31 | +## Style |
| 32 | + |
| 33 | +### Stylesheets |
| 34 | + |
| 35 | +You don't need to import loaders stylesheets. By default they are loaded after first use of a loader component. |
| 36 | + |
| 37 | +If you still want to load css yourself, you can cancel default behavior by setting `useDefaultStyle={false}`. |
| 38 | +and load it from stylesheets that are included in this package. |
| 39 | + |
| 40 | +### Use of space |
| 41 | + |
| 42 | +We found it useful for loaders to center itself and expand to take all possible area around. |
| 43 | + |
| 44 | +If you want to position loader yourself you can set prop `shouldExpand={false}`. |
| 45 | + |
| 46 | +# Example |
| 47 | +1. Clone this repository |
| 48 | +2. Install packages in simplr-loaders root directory: |
| 49 | +```sh |
| 50 | +$ npm install |
| 51 | +``` |
| 52 | +3. Go to 'example' folder and run: |
| 53 | +```sh |
| 54 | +$ npm run example && npm run start |
| 55 | +``` |
| 56 | + |
| 57 | +# API |
| 58 | + |
| 59 | +## LoaderBase |
| 60 | + |
| 61 | +```ts |
| 62 | +export abstract class LoaderBase<TProps extends BaseProps, TState> extends React.PureComponent<TProps, TState> |
| 63 | +``` |
| 64 | +`LoaderBase` is a base class of all loader components in `simplr-loaders`. |
| 65 | +
|
| 66 | +### `protected abstract LoaderId: string;` |
| 67 | +Unique identifier of a loader. |
| 68 | +
|
| 69 | +### `protected StylesClass: string;` |
| 70 | +Default className of a loader component. If not declared, `LoaderId` will be taken as default className. |
| 71 | +
|
| 72 | +### `protected AppendStyles(styles: string, props: TProps)` |
| 73 | +Puts stringified stylesheet of a loader into the `<head>`. |
| 74 | +
|
| 75 | +`styles: string` - stringified stylesheet. |
| 76 | +
|
| 77 | +`props: TProps` - component props. |
| 78 | +
|
| 79 | +### `protected get AggregatedClassName()` |
| 80 | +Aggregates all classNames of a loader: |
| 81 | +- default className defined in `StylesClass` |
| 82 | +- `props.className` |
| 83 | +- className that defines how loader use the space (whether it's size should be expanded or reduced). |
| 84 | +
|
| 85 | +## BaseProps |
| 86 | +Base props of all loader components in simplr-loaders. |
| 87 | +
|
| 88 | +```ts |
| 89 | +export interface BaseProps { |
| 90 | + /** |
| 91 | + * Custom class name of a loader. |
| 92 | + * |
| 93 | + * @type {string} |
| 94 | + * @memberOf BaseProps |
| 95 | + */ |
| 96 | + className?: string; |
| 97 | + /** |
| 98 | + * Specifies whether style should be placed in <head>. |
| 99 | + * This is a default behavior you can cancel. |
| 100 | + * |
| 101 | + * @type {boolean} |
| 102 | + * @memberOf BaseProps |
| 103 | + */ |
| 104 | + useDefaultStyle?: boolean; |
| 105 | + /** |
| 106 | + * Specifies whether the loader should expand to take all possible area. |
| 107 | + * Default value `true`. |
| 108 | + * Using this prop you can cancel the default behavior. |
| 109 | + * In that case the loader will take an area of a strict size. |
| 110 | + * |
| 111 | + * @type {boolean} |
| 112 | + * @memberOf BaseProps |
| 113 | + */ |
| 114 | + shouldExpand?: boolean; |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +# Contribution |
| 119 | + |
| 120 | +If you want to add loader to this package you'll need to know basic concepts of: |
| 121 | +- [`TypeScript`](https://www.typescriptlang.org/docs/tutorial.html) |
| 122 | +- [`SCSS`](http://sass-lang.com/guide) |
| 123 | +- [`React`](https://facebook.github.io/react/tutorial/tutorial.html) |
| 124 | + |
| 125 | +In `src/your-loader-folder` should be placed three files: |
| 126 | +- Stylesheet written in SCSS; |
| 127 | +- React component of a loader written in TypeScript; |
| 128 | +- TypeScript file with exported stringified stylesheet of your loader (automatically generated with [`css-to-ts`](https://www.npmjs.com/package/css-to-ts) when building). |
| 129 | + |
| 130 | +## Component |
| 131 | + |
| 132 | +Use TypeScript to create component class: |
| 133 | + |
| 134 | +```ts |
| 135 | +import * as React from "react"; |
| 136 | + |
| 137 | +// `MyLoaderStyle` TypeScript file with stringified css will be generated from your stylesheet when build is started. |
| 138 | +// But you still have to import it. |
| 139 | +import { MyLoaderStyle } from "./spinner-loader-style"; |
| 140 | + |
| 141 | +import { LoaderBase, BaseProps } from "../abstractions/loader-base"; |
| 142 | + |
| 143 | +// Loader MUST extend LoaderBase class. |
| 144 | +// Every loader MUST have props that are defined in BaseProps. |
| 145 | +// If you need more props, you can create your own interface with BaseProps extended. |
| 146 | +export class MyLoader extends LoaderBase<BaseProps, {}> { |
| 147 | + |
| 148 | + // Load style using `AppendStyles` from LoaderBase. |
| 149 | + constructor(props: BaseProps, context: any) { |
| 150 | + super(props, context); |
| 151 | + this.AppendStyles(MyLoaderStyle, props); |
| 152 | + } |
| 153 | + |
| 154 | + // Define loader id. |
| 155 | + // Id is used to identify style in `<head>`. |
| 156 | + protected LoaderId: string = "my-loader"; |
| 157 | + |
| 158 | + // This property is optional. |
| 159 | + // If you don't define `StylesClass`, `LoaderId` will be taken as 'StylesClass'. |
| 160 | + protected StylesClass: string = "class-name-of-my-loader"; |
| 161 | + |
| 162 | + // Define containers structure that your loader requires. |
| 163 | + render() { |
| 164 | + // Use `AggregatedClassName` from `LoaderBase` to define all classNames of your loader. |
| 165 | + return <div className={this.AggregatedClassName}> |
| 166 | + <div></div> |
| 167 | + <div></div> |
| 168 | + </div>; |
| 169 | + } |
| 170 | +} |
| 171 | +``` |
| 172 | + |
| 173 | +## StyleSheet |
| 174 | +Use SCSS to write a loader's stylesheet. |
| 175 | + |
| 176 | +Next to all styles of your loader you should implement two additional classNames: |
| 177 | + |
| 178 | +- `.expanded` when loader takes all area of parrent container (default). |
| 179 | +- `.reduced` when loader takes strict area. Basically it should contain specified height and width. |
| 180 | + |
| 181 | +# License |
| 182 | +Released under the [MIT license](LICENSE). |
0 commit comments