|
| 1 | +# Quick Start |
| 2 | + |
| 3 | +This guide will teach you how to create and use V4Fire components. |
| 4 | + |
| 5 | +## Creating a component |
| 6 | + |
| 7 | +``` |
| 8 | +src |
| 9 | +└── components |
| 10 | + └── b-example |
| 11 | + ├── b-example.ts |
| 12 | + ├── b-example.ss |
| 13 | + ├── b-example.styl |
| 14 | + └── index.js |
| 15 | +``` |
| 16 | + |
| 17 | +It's recommended to create components inside the `src/components` directory. |
| 18 | +Typical component consist of these files: |
| 19 | + |
| 20 | +- b-example.ts - class declaration of the component |
| 21 | +- b-example.ss - component's template |
| 22 | +- b-example.styl - component's styles |
| 23 | +- index.js - declares component's parent, dependencies, libraries |
| 24 | + |
| 25 | +> "b" stands for a block from the BEM methodology |
| 26 | +
|
| 27 | +**Important** |
| 28 | + |
| 29 | +Components are declared globally, so there cannot be 2 components with the same name. |
| 30 | +It's recommended to use prefix in a component's name, for example: `b-main-head`, `b-catalog-dialog`, etc. |
| 31 | + |
| 32 | +### index.js |
| 33 | + |
| 34 | +The `index.js` is the most important file of a component: it allows to statically |
| 35 | +analyze all components of the project and their dependencies. |
| 36 | + |
| 37 | +All components must extend base component `i-block` and should specify |
| 38 | +components used in their template as dependencies, therefore `index.js` |
| 39 | +should look like that: |
| 40 | + |
| 41 | +```js |
| 42 | +package('b-example') |
| 43 | + .extends('i-block') |
| 44 | + .dependencies('b-button'); |
| 45 | +``` |
| 46 | + |
| 47 | +### b-example.ts |
| 48 | + |
| 49 | +A component is declared using a class, which is decorated with the `@component` decorator. |
| 50 | +To define props of the component the `@prop` decorator is used. |
| 51 | +Reactive properties, which will force component to re-render, are defined using the `@field` decorator. |
| 52 | + |
| 53 | +```ts |
| 54 | +import iBlock, { component, prop, field } from 'components/super/i-block/i-block'; |
| 55 | +export * from 'components/super/i-block/i-block'; |
| 56 | + |
| 57 | +@component() |
| 58 | +export default class bExample extends iBlock { |
| 59 | + @prop(Number) |
| 60 | + a: number; |
| 61 | + |
| 62 | + @prop(Number) |
| 63 | + b: number; |
| 64 | + |
| 65 | + @prop({ |
| 66 | + type: String, |
| 67 | + required: false, |
| 68 | + validator: (value: string) => ['+', '-', '/', '*'].includes(value) |
| 69 | + }) |
| 70 | + |
| 71 | + op: string = '+'; |
| 72 | + |
| 73 | + @field() |
| 74 | + result: number; |
| 75 | + |
| 76 | + calc(): void { |
| 77 | + this.result = Function('a', 'b', `return a ${this.op} b`)(this.a, this.b); |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +### b-example.ss |
| 83 | + |
| 84 | +Template of the component is defined using a [Snakeskin](http://snakeskintpl.github.io/docs/index.html) language. |
| 85 | + |
| 86 | +```ss |
| 87 | +- namespace [%fileName%] |
| 88 | +
|
| 89 | +- include 'components/super/i-block'|b as placeholder |
| 90 | +
|
| 91 | +- template index() extends ['i-block'].index |
| 92 | + - block body |
| 93 | + < b-button @click = calc |
| 94 | + Calculate! |
| 95 | +
|
| 96 | + /// It will become a <div class="b-example__result"></div> |
| 97 | + < .&__result |
| 98 | + /// Runtime interpolation is declared using double curly brackets {{ ... }} |
| 99 | + a {{ op }} b = {{ result }} |
| 100 | +``` |
| 101 | + |
| 102 | +The main template of the component must be named `index`. |
| 103 | + |
| 104 | +> It is possible to declare multiple templates for a component, |
| 105 | +> but only the `index` template will be used for the `render` function of the component. |
| 106 | +
|
| 107 | +It's important that component's template must have the `namespace` directive with `[%fileName%]` macro, |
| 108 | +which will automatically create a namespace `b-example`. |
| 109 | + |
| 110 | +> We use Vue.js to bind component's data to the template. It means that you |
| 111 | +> can use standard Vue directives like `v-for`, `v-if`, `v-model`, etc. |
| 112 | +
|
| 113 | +### b-example.styl |
| 114 | + |
| 115 | +Styles of the component are defined using the [Stylus](https://stylus-lang.com/) language |
| 116 | +with custom [plugin for inheritance](https://github.com/pzlr/stylus-inheritance). |
| 117 | + |
| 118 | +```styl |
| 119 | +@import "components/super/i-block/i-block.styl" |
| 120 | +
|
| 121 | +// This object is associated with the component and can be inherited. |
| 122 | +// Properties from i-block will be added to this object. |
| 123 | +$p = { |
| 124 | + color: #F00 |
| 125 | +} |
| 126 | +
|
| 127 | +b-example extends i-block |
| 128 | + &__result |
| 129 | + color $p.color |
| 130 | +``` |
| 131 | + |
| 132 | +## Using a component |
| 133 | + |
| 134 | +Now you can easily use created component inside other component, for example: |
| 135 | + |
| 136 | +```ss |
| 137 | +/// src/pages/p-root/p-root.ss |
| 138 | +
|
| 139 | +- namespace [%fileName%] |
| 140 | +
|
| 141 | +- include 'components/super/i-static-page/i-static-page.component.ss'|b as placeholder |
| 142 | +
|
| 143 | +- template index() extends ['i-static-page.component'].index |
| 144 | + - block body |
| 145 | + < b-example :a = 1 | :b = 2 | :op = '-' |
| 146 | +``` |
| 147 | + |
| 148 | +Don't forget to add it to `p-root` dependencies: |
| 149 | + |
| 150 | +```js |
| 151 | +// src/pages/p-root/index.js |
| 152 | +package('p-root') |
| 153 | + .extends('i-static-page') |
| 154 | + .dependencies('b-example'); |
| 155 | +``` |
| 156 | + |
| 157 | +> "p" stands for page |
0 commit comments