@startinblox/boilerplate 3.0.2 → 3.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +205 -42
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,85 +1,248 @@
1
- # Boilerplate
1
+ # Startin'blox components repository boilerplate
2
2
 
3
- ## Table of Contents
3
+ ## Introduction
4
4
 
5
- 1. [Introduction](#introduction)
6
- 2. [Installation](#installation)
7
- 3. [Development](#development)
8
- 4. [Tests](#tests)
9
- 5. [Storybook](#storybook)
10
- 6. [Build](#build)
11
- 7. [License](#license)
5
+ This repository provides a boilerplate for developing Startin'blox web components. It streamlines the creation and deployment of web components, whether used independently or integrated with the Startin'blox ecosystem, including Lit, Startin'blox Core, Startin'blox Store, Startin'blox Router, and Startin'blox Orbit. The primary objective is to enable developers to concentrate on web component logic by abstracting common complexities and providing out-of-the-box functionalities.
12
6
 
13
- ## Introduction
7
+ ## Features
14
8
 
15
- This is a boilerplate of startin'blox component.
9
+ This boilerplate includes:
16
10
 
17
- ## Installation
11
+ * **Localization:** Compatibility with platforms like Weblate, powered by [`@lit/localize`](https://www.npmjs.com/package/@lit/localize).
12
+ * **Data Management:** A comprehensive wrapper around the Startin'blox Store for simplified property handling (RDF or named).
13
+ * **Component Awareness:** Mechanisms to manage race conditions and inheritance across external components.
14
+ * **Helper Utilities:** Functions for data reactivity, filtering, and sorting.
15
+ * **Development Tools:**
16
+ * **Storybook:** For component documentation and isolated development, reducing the need for a heavy local environment.
17
+ * **Cypress:** For component testing.
18
+ * **Unpluggin Icons:** Access to a vast library of icons from [Iconify](https://icon-sets.iconify.design/).
19
+ * **Lit Integration:** Pre-made component classes to accelerate development.
20
+ * **Vite-powered:** Typescript, SASS, PostCSS, Autoprefixer... Focus on your component logic, not the build process.
18
21
 
19
- ```bash
20
- npm install
21
- ```
22
+ ## Getting started
22
23
 
23
- ## Development
24
+ ### Creating a new components repository
24
25
 
25
- ```bash
26
- npm run watch
27
- ```
26
+ To initialize a new component repository from this boilerplate:
28
27
 
29
- ## Tests
28
+ 1. Clone this repository.
29
+ 2. Rename the `origin` remote to `upstream`:
30
30
 
31
- ```bash
32
- npm run cy:run
33
- ```
31
+ ```bash
32
+ git remote rename origin upstream
33
+ ```
34
34
 
35
- or
35
+ 3. Update the `name`, `description`, and `repository.url` fields in [`package.json`](package.json).
36
+ 4. Commit the changes with a `major: Initial commit` message.
37
+ 5. Create a new repository on [git.startinblox.com/components](https://git.startinblox.com/components/).
38
+ 6. Add the new repository as the `origin` remote:
36
39
 
37
- ```bash
38
- npm run cy:open
40
+ ```bash
41
+ git remote add origin https://git.startinblox.com/components/your-component-name.git
42
+ ```
43
+
44
+ 7. Configure Gitlab settings (branch protection rules, FF merge only, clone instead of fetch).
45
+ 8. Push to `origin master`.
46
+
47
+ For production usage, components can be served via a CDN like JSDelivr or self-hosted.
48
+
49
+ ```html
50
+ <script type="module" src="https://cdn.jsdelivr.net/npm/@startinblox/core@latest/dist/store.js"></script>
51
+ <script type="module" src="https://cdn.jsdelivr.net/npm/your-component-name@latest"></script>
52
+ <!-- ... -->
53
+ <your-component-1 data-src="..."></your-component-1>
54
+ <your-component-2>Hello World!</your-component-2>
55
+ <!-- etc. -->
39
56
  ```
40
57
 
41
- ## Storybook
58
+ ### Installation
59
+
60
+ To set up the local development environment with Storybook:
42
61
 
43
62
  ```bash
63
+ npm install
44
64
  npm run storybook
45
65
  ```
46
66
 
47
- ## Build
67
+ ## Development environment
48
68
 
49
- ```bash
50
- npm run build
69
+ This boilerplate is designed as a **library** for multiple web components and intentionally **does not include** a default `index.html` file.
70
+
71
+ **Storybook** is the primary development environment for isolated component development and documentation. **Cypress** is integrated for component testing.
72
+
73
+ For components intended to be packaged within the broader Startin'blox environment, integrating with a local Orbit instance is recommended.
74
+
75
+ ### Updating from the boilerplate
76
+
77
+ To keep your component repository synchronized with updates from this boilerplate:
78
+
79
+ 1. Ensure `upstream` is configured to point to this boilerplate's repository.
80
+ 2. Pull changes from the boilerplate's `upstream`:
81
+
82
+ ```bash
83
+ git pull upstream master
84
+ ```
85
+
86
+ 3. Resolve any merge conflicts. Changes outside the boilerplate's core code should merge smoothly.
87
+ 4. Push the updates to `origin master`.
88
+
89
+ ## Core Concepts
90
+
91
+ ### Pre-made component classes
92
+
93
+ * `OrbitComponent`: A component designed to display or handle a Resource or Container, optionally extending its functionality for Orbit integration.
94
+ * `ObjectsHandler`: A component for rendering an array of objects, typically used within an `OrbitComponent` to display a Container.
95
+ * `ObjectHandler`: A component for rendering a single object, commonly used within an `OrbitComponent` or `ObjectsHandler` to display a Resource.
96
+
97
+ ### Store reactivity
98
+
99
+ * `setupCacheInvalidation`: Invalidates the store cache and updates the component when a `save` event occurs for resources listed by specified attributes and keywords.
100
+ * `setupOnResourceReady`: Invalidates the store cache and updates the component upon a `resourceReady` event, independent of attributes.
101
+ * `setupOnSaveReset`: Resets form values by calling the `_setValue` method (which must be custom-implemented) on a `save` event containing specified keywords.
102
+
103
+ Custom cache invalidation methods can be added:
104
+
105
+ ```javascript
106
+ async _afterAttach() {
107
+ this._subscriptions.add(["name-of-the-event", (e) => {
108
+ // Logic to execute when `name-of-the-event` occurs
109
+ }]);
110
+ // Ensure event deduplication before re-subscribing
111
+ this._subscribe();
112
+ // _afterAttach expects a Promise, you can reject on error
113
+ return Promise.resolve();
114
+ }
51
115
  ```
52
116
 
53
- ## Localization
117
+ ### `OrbitComponent` default attributes and methods
54
118
 
55
- This project uses [`@lit/localize`](https://www.npmjs.com/package/@lit/localize) for internationalization.
119
+ **Attributes:**
56
120
 
57
- ### Extracting strings
121
+ * `defaultDataSrc`: (Optional) Used within Lit Tasks to track the original data source when the component's `dataSrc` is rewritten.
122
+ * `dataSrc`: (Optional) Specifies the resource to display within a Lit Task.
123
+ * `nestedField`: (Optional) Requires manual handling within a Lit Task.
124
+ * `uniq`: (Optional) Allows multiple instances of the component within the same DOM.
125
+ * `route`: (Optional) Enables router-awareness, preventing rendering/updating when the current route does not match expectations.
126
+ * `cherryPickedProperties`: (Required) An array describing the expected RDF or named properties for the component.
58
127
 
59
- To extract strings for translation, run:
128
+ **Methods:**
60
129
 
61
- ```bash
62
- npm run locale:extract
130
+ * `render`: A Lit method; refer to Lit documentation. Should return a Lit Task or `nothing`.
131
+ * `_afterAttach`: An asynchronous method called after the component is attached to the DOM and fully initialized. Expects a resolved or rejected Promise.
132
+ * `_navigate(e)`: Facilitates navigation using the router and predefined attributes within a `render` method.
133
+ * `_getProxyValue(dataSrc)`: An asynchronous method typically called within a Lit Task. It retrieves data based on `cherryPickedProperties`, is recursive by default, and allows `cherryPickedProperties` to be overridden.
134
+ * `_responseAdaptator(res)`: An asynchronous method called before `_getProxyValue` returns its result, allowing for response adaptation (e.g., prefixing a `name` property based on `type`).
135
+ * `gatekeeper`: A pre-written method for components intended to operate exclusively within Orbit, simplifying gatekeeping logic.
136
+
137
+ `OrbitComponent` inherits methods from `ObjectsHandler`.
138
+
139
+ ### `ObjectsHandler` methods
140
+
141
+ * `hasType(type)`: Checks if any object within `this.objects` shares the requested type, returning a boolean.
142
+ * `renderTemplateWhenWith(property, callback)`: Renders the `callback` if the specified property (or properties) is present. Nested arrays enable `OR` testing, while `AND` testing is implicit. Example: `[["name", "shortname"], "description"]` returns the callback if (`name` OR `shortname`) AND `description` are present.
143
+
144
+ ### `ObjectHandler` methods
145
+
146
+ * `isType(type)`: Checks if the object shares the requested type, returning a boolean.
147
+ * `renderTemplateWhenWith`: Functions identically to the `ObjectsHandler` method but applies to a single object.
148
+
149
+ ### Available helpers
150
+
151
+ The `src/helpers` directory contains various utilities:
152
+
153
+ * `datas`:
154
+ * `dataBuilder`: For creating mock data.
155
+ * `filterGenerator`: For seamless object filtering using `filterObjectByX` methods.
156
+ * `sort`: A general-purpose sorting method.
157
+ * `ui`:
158
+ * `formatDate`: For date formatting using native `Intl` methods.
159
+ * `lipsum`: For configuring mock word, sentence, or paragraph generation.
160
+ * `utils`:
161
+ * `requestNavigation`: A wrapper around the Startin'blox Router for JavaScript-based navigation.
162
+ * `uniq`: A unique ID generator.
163
+
164
+ ### `cherryPickedProperties` explained
165
+
166
+ The `cherryPickedProperties` array within an `OrbitComponent` defines the properties expected by the component. Each entry can have up to four attributes:
167
+
168
+ * `key`: The original property name or RDF.
169
+ * `value`: The name this property will have within the component.
170
+ * `cast`: A callback function (e.g., `formatDate` helper) to transform the property's value.
171
+ * `expand`: If `true`, and the property contains a Resource or Container, it will be recursively expanded using the same `cherryPickedProperties`. Exercise caution with circular dependencies; `_responseAdaptator` is often more suitable for such cases, allowing `_getProxyValue` to be called with `recursive=False` or different `cherryPickedProperties`. Defaults to `false`.
172
+
173
+ **Example:**
174
+
175
+ ```typescript
176
+ cherryPickedProperties: PropertiesPicker[] = [
177
+ { key: "name", value: "name" },
178
+ // Using the project property two times: One with a cast to keep its @id, and one expanded giving complete access to its datas
179
+ { key: "project", value: "projectId", cast: (r) => r["@id"] },
180
+ // This will expand the `project` property, using the same cherryPickedProperties, so it'll contain any `project.name`, `project.project` and `project.description`
181
+ { key: "project", value: "project", expand: true },
182
+ { key: "description", value: "description" },
183
+ // In any case, the `_originalResource`, the `@id`, `@context` and `@type` will be fetched and accessible
184
+ ];
63
185
  ```
64
186
 
65
- This command will generate XLIFF files in the `locales` directory.
187
+ ## Development workflow
188
+
189
+ ### Localization
66
190
 
67
- ### Changing locale
191
+ This project utilizes [`@lit/localize`](https://www.npmjs.com/package/@lit/localize) for internationalization.
68
192
 
69
- To change the locale, run:
193
+ 1. Configure [`lit-localize.json`](lit-localize.json), specifically the `targetLocales` key.
194
+ 2. Run `npm run locale:extract` to generate initial localization files.
195
+ 3. Execute `npm run locale:extract` to update localization files whenever `str` or `msg` methods from Lit Localize are used. XLIFF files will be generated in the `locales` directory.
196
+ 4. Before production deployment, run `npm run locale:build` to generate built localization files.
197
+
198
+ To change the locale at runtime within an application:
70
199
 
71
200
  ```javascript
72
201
  window.setLocale.map((setLocale) => setLocale("your-lang-code"));
73
202
  ```
74
203
 
75
- ### Getting current locale
76
-
77
- To get the current locale, run:
204
+ To retrieve the current locale:
78
205
 
79
206
  ```javascript
80
207
  window.getLocale.map((locale) => console.log(locale));
81
208
  ```
82
209
 
210
+ ### Testing
211
+
212
+ ```bash
213
+ # Run all tests
214
+ npm run cy:run
215
+ # Open Cypress's interface
216
+ npm run cy:open
217
+ ```
218
+
219
+ ### Building for production
220
+
221
+ ```bash
222
+ # Generate localization files
223
+ npm run locale:build
224
+ # Generate Storybook documentation
225
+ npm run build-storybook
226
+ # Build the component repository
227
+ npm run build
228
+ ```
229
+
230
+ ## Orbit integration
231
+
232
+ Components built with this boilerplate are designed to work seamlessly with Orbit without requiring specific configurations.
233
+
234
+ To prevent potential race conditions, include `import @src/initializer;` at the top of your component's file, before the `@customElement` declaration.
235
+
236
+ Application performance can be optimized by utilizing the `gatekeeper` method within your `OrbitComponent`'s render methods.
237
+
238
+ Note that without a complete Orbit environment, interactions with external components (e.g., route management, component deduplication, global localization, conditional component display) may be limited.
239
+
240
+ ## Best practices
241
+
242
+ Adhere to general web component usage rules, as outlined by [MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/API/Web_components) or the [webcomponents.org guideline](https://www.webcomponents.org/introduction).
243
+
244
+ Tailor web components to their expected application environment. Features like the gatekeeper and localization usage should be standard practice, not exceptions. This approach ensures components are efficient and integrate effectively with the broader application and other components.
245
+
83
246
  ## License
84
247
 
85
248
  [MIT](LICENSE)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@startinblox/boilerplate",
3
- "version": "3.0.2",
3
+ "version": "3.0.3",
4
4
  "description": "Startin'blox Boilerplate",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",