@customviews-js/customviews 1.1.0 → 1.1.1

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.
package/README.md CHANGED
@@ -1,220 +1,363 @@
1
- # Custom Views
2
-
3
- v1.1.0
4
-
5
- [npm package link](https://www.npmjs.com/package/@customviews-js/customviews)
6
-
7
- A JavaScript library for creating contextual, adaptive web content. Perfect for educational websites, documentation, and multi-audience platforms.
8
-
9
- ## Quick Start
10
-
11
- ### HTML Setup
12
-
13
- ```html
14
- <!-- Mark content with data-cv-toggle attribute -->
15
- <div data-cv-toggle="beginner">
16
- <p>Beginner content here</p>
17
- </div>
18
-
19
- <div data-cv-toggle="advanced">
20
- <p>Advanced content here</p>
21
- </div>
22
-
23
- <!-- Dynamic content with ID -->
24
- <div data-cv-toggle="beginner" data-cv-id="intro-guide"></div>
25
- ```
26
-
27
- ### Simple Setup
28
-
29
- ```html
30
- <script src="https://unpkg.com/@customviews-js/customviews"></script>
31
- ```
32
-
33
- This auto-init script will:
34
- 1. Automatically load configuration from `/customviews.config.json`
35
- 2. Initialize the core library
36
- 3. Create and render the widget if enabled
37
-
38
- #### Configuration File
39
-
40
- Create a `customviews.config.json` file in your site root:
41
-
42
- ```json
43
- {
44
- "core": {
45
- "config": {
46
- "allToggles": ["beginner", "advanced"],
47
- "defaultState": {
48
- "toggles": ["beginner"]
49
- }
50
- },
51
- "assetsJsonPath": "/assets.json"
52
- },
53
- "widget": {
54
- "enabled": true,
55
- "position": "middle-left",
56
- "showWelcome": true
57
- }
58
- }
59
- ```
60
-
61
- #### Script Attributes
62
-
63
- The auto-init script supports these data attributes:
64
- - `data-base-url`: Base URL for all paths (e.g., `/customviews`)
65
- - `data-config-path`: Custom path to config file (default: `/customviews.config.json`)
66
-
67
- ```html
68
- <script
69
- src="https://unpkg.com/@customviews-js/customviews"
70
- data-base-url="/my-site"
71
- data-config-path="/configs/custom-views.json">
72
- </script>
73
- ```
74
-
75
- ## Widget Features
76
-
77
- - **Floating Widget**: Rounded rectangle design with gear icon
78
- - **Six Positions**: top-left, top-right, bottom-left, bottom-right, middle-left, middle-right
79
- - **Welcome Modal**: Optional first-visit modal (localStorage cached)
80
- - **Theme Support**: Light and dark themes
81
- - **URL Sharing**: Generate shareable URLs with custom states
82
-
83
- ## Core Initialization Options
84
-
85
- ```typescript
86
- {
87
- config?: Config; // Config object with allToggles and defaultState
88
- configPath?: string; // Path to JSON config file
89
- assetsJsonPath?: string; // Path to JSON assets file
90
- baseURL?: string; // Base URL prepended to all paths (e.g., '/customviews')
91
- rootEl?: HTMLElement; // Root element to apply custom views (default: document.body)
92
- }
93
- ```
94
-
95
- **Notes**:
96
- - Either `config` (literal config object) or `configPath` must be provided.
97
- - `baseURL` is automatically prepended to `configPath`, `assetsJsonPath`, and all asset `src` paths.
98
- - Absolute URLs (starting with `http://` or `https://`) are not modified.
99
-
100
- ## Configuration File Format
101
-
102
- When using auto-initialization, the config file follows this structure:
103
-
104
- ```typescript
105
- {
106
- core: {
107
- config?: any; // Core configuration object
108
- configPath?: string; // Path to the configuration file
109
- assetsJsonPath?: string; // Path to the assets JSON file
110
- baseURL?: string; // Base URL for all paths
111
- };
112
-
113
- widget?: {
114
- enabled?: boolean; // Whether the widget is enabled
115
- position?: string; // Widget position
116
- theme?: 'light' | 'dark'; // Widget theme
117
- showReset?: boolean; // Whether to show reset button
118
- title?: string; // Widget title
119
- description?: string; // Widget description text
120
- showWelcome?: boolean; // Whether to show welcome modal on first visit
121
- welcomeTitle?: string; // Welcome modal title
122
- welcomeMessage?: string; // Welcome modal message
123
- };
124
- }
125
- ```
126
-
127
- ## Events
128
-
129
- ### `customviews:ready` Event
130
-
131
- When using auto-initialization, a `customviews:ready` event is dispatched when CustomViews is fully initialized:
132
-
133
- ```javascript
134
- document.addEventListener('customviews:ready', (event) => {
135
- const { core, widget } = event.detail;
136
-
137
- // You can now access the core and widget instances
138
- console.log('CustomViews initialized!');
139
-
140
- // Example: Programmatically change state
141
- core.applyState({ toggles: ['advanced'] });
142
- });
143
- ```
144
-
145
- ### Global Instance Access
146
-
147
- When using auto-initialization, the core and widget instances are stored on the `window` object:
148
-
149
- ```javascript
150
- // Access the core instance
151
- const core = window.customViewsInstance.core;
152
-
153
- // Access the widget instance (if enabled)
154
- const widget = window.customViewsInstance.widget;
155
-
156
- // Example: Programmatically change state
157
- core.applyState({ toggles: ['advanced'] });
158
- ```
159
-
160
- ## Widget Options
161
-
162
- ```typescript
163
- {
164
- core: CustomViewsCore; // Required: core instance
165
- position?: string; // Default: 'middle-left'
166
- theme?: 'light' | 'dark'; // Default: 'light'
167
- showReset?: boolean; // Default: true
168
- showWelcome?: boolean; // Default: false
169
- welcomeTitle?: string; // Custom welcome title
170
- welcomeMessage?: string; // Custom welcome message
171
- title?: string; // Widget tooltip
172
- description?: string; // Widget modal description
173
- }
174
- ```
175
-
176
- ## Assets JSON Setup
177
-
178
- The `assets.json` file defines reusable content that can be dynamically injected into your pages. Each asset is referenced by a unique key and can contain images, HTML, or text.
179
- * Note that `baseURL` defined during initialization is automatically prepended to all asset `src` paths.
180
-
181
- ### Structure
182
-
183
- ```json
184
- {
185
- "htmlContent": {
186
- "content": "<h2>Dynamic HTML</h2><p>This content is injected dynamically.</p>"
187
- },
188
- "textContent": {
189
- "content": "Plain text content"
190
- },
191
- "imageContent": {
192
- "src": "/assets/mac.png"
193
- },
194
- "assetKey": {
195
- "src": "/path/to/image.png",
196
- "alt": "Description",
197
- "className": "custom-class",
198
- "style": "border-radius: 8px;"
199
- },
200
- }
201
- ```
202
-
203
- ### Asset Properties
204
-
205
- - **`src`**: Image URL (makes it an image asset), **`content`**: Text or HTML content (auto-detected)
206
- - **`alt`**: Alt text for images, **`className`**: CSS classes to apply, **`style`**: Inline CSS styles
207
-
208
- ### Usage in HTML
209
-
210
- ```html
211
- <!-- Reference asset by key using data-cv-id (or data-customviews-id for backward compatibility) -->
212
- <div data-cv-toggle="beginner" data-cv-id="assetKey"></div>
213
- ```
214
-
215
- When the toggle is active, the asset will be automatically rendered into the element.
216
-
217
-
218
- ## License
219
-
1
+ ## Custom Views
2
+
3
+ [npm package link](https://www.npmjs.com/package/@customviews-js/customviews)
4
+
5
+ _Custom Views_ allows developers and designers to define reusable content views that can be toggled, personalized, or adapted dynamically for different users and contexts. It is framework-agnostic, meaning it works with plain HTML, JavaScript, or alongside modern frameworks without imposing additional dependencies.
6
+
7
+ With Custom Views, you can:
8
+
9
+ - Show or hide sections of a page based on user preferences.
10
+ - Persist user-selected content variants (e.g., "CLI view" vs "GUI view").
11
+ - Enhance accessibility and provide tailored experiences without bloating your site.
12
+
13
+ Whether you are building a static site, a dashboard, or a documentation portal, Custom Views gives you the tools to make your content interactive and adaptable while keeping things lightweight and simple.
14
+
15
+ [:fa-brands-github: GitHub Link&nbsp; :fa-solid-arrow-up-right-from-square:](https://github.com/customviews-js/customviews)
16
+
17
+ ## Components
18
+
19
+ Custom Views provides two main components:
20
+
21
+ - **Toggles**: Show or hide sections of a page based on categories (e.g., platform-specific content).
22
+ - **Tabs**: Create mutually exclusive, synchronized content sections for different views.
23
+
24
+ Both components work out of the box without configuration, but can be enhanced with the widget and config file for better user experience.
25
+
26
+ ## Quick Start
27
+
28
+ ### Mark content with toggles
29
+
30
+ Use the `data-cv-toggle` attribute or `<cv-toggle>` element to mark content that should be shown only for specific categories (for example `mac`, `linux`, `windows`).
31
+
32
+ ```html
33
+ <!-- Mark content with data-cv-toggle attribute -->
34
+ <div data-cv-toggle="mac">
35
+ <p>Mac content here</p>
36
+ </div>
37
+
38
+ <div data-cv-toggle="linux">
39
+ <p>Linux content here</p>
40
+ </div>
41
+
42
+ <div data-cv-toggle="windows">
43
+ <p>Windows content here</p>
44
+ </div>
45
+
46
+ <!-- Or use the cv-toggle element -->
47
+ <cv-toggle category="mac">
48
+ <p>Mac content here</p>
49
+ </cv-toggle>
50
+
51
+ <!-- Dynamic content with ID -->
52
+ <div data-cv-toggle="mac" data-cv-id="mac-picture"></div>
53
+ ```
54
+
55
+ You can apply multiple toggles to a single element by separating categories with spaces. This allows content to appear as long as one toggle category is active.
56
+
57
+ ```html
58
+ <div data-cv-toggle="mac linux">
59
+ This section appears for both macOS and Linux users.
60
+ </div>
61
+
62
+ <cv-toggle category="mac linux">
63
+ This section appears for both macOS and Linux users.
64
+ </cv-toggle>
65
+ ```
66
+
67
+ ### Attributes & Options
68
+
69
+ | Name | Type | Default | Description |
70
+ |------|------|----------|-------------|
71
+ | `data-cv-toggle` | string | **required** for data attribute usage | Defines the category for the element. Example: `data-cv-toggle="mac"`. |
72
+ | `category` | string | **required** for `<cv-toggle>` | Defines the category for the cv-toggle element. Example: `category="mac"`. |
73
+ | `data-cv-id` / `data-customviews-id` | string | - | Marks the element as an asset render target. When visible, matching assets from `assets.json` will be dynamically inserted. |
74
+
75
+ **Configuration Integration**
76
+ To expose toggles in the widget and provide defaults, list them in your config file under `config.allToggles` and set `config.defaultState.toggles`:
77
+
78
+ ```json
79
+ {
80
+ "config": {
81
+ "allToggles": ["mac", "linux", "windows"],
82
+ "defaultState": { "toggles": ["mac"] }
83
+ }
84
+ }
85
+ ```
86
+
87
+
88
+ ## Tabs
89
+
90
+ Tabs let you create mutually exclusive, synchronized content sections using the custom elements `<cv-tabgroup>` and `<cv-tab>`. All tab groups on the page that share the same `id` stay in sync — changing a tab in one group updates the others automatically.
91
+
92
+ Basic example with synchronized groups:
93
+
94
+ ```html
95
+ <cv-tabgroup id="fruit">
96
+ <cv-tab id="apple" header="Apple">
97
+
98
+ **Apple Information**
99
+
100
+ Apples are crisp, sweet fruits that come in many varieties. They are rich in fiber and vitamin C.
101
+
102
+ </cv-tab>
103
+ <cv-tab id="orange" header="Orange">
104
+
105
+ **Orange Information**
106
+
107
+ Oranges are citrus fruits known for their high vitamin C content and refreshing juice.
108
+
109
+ </cv-tab>
110
+ <cv-tab id="pear" header="Pear">
111
+
112
+ **Pear Information**
113
+
114
+ Pears are sweet, bell-shaped fruits with a soft texture when ripe. They're high in fiber and antioxidants.
115
+
116
+ </cv-tab>
117
+ </cv-tabgroup>
118
+
119
+ <cv-tabgroup id="fruit" nav="auto">
120
+ <cv-tab id="apple" header="Apple Types">
121
+
122
+ Apple types include **Granny Smith** and the **Cosmic Crisp**.
123
+
124
+ </cv-tab>
125
+ <cv-tab id="orange" header="Orange Types">
126
+
127
+ Orange types include the **Blood orange** and **Valencia orange**.
128
+ </cv-tab>
129
+ <cv-tab id="pear" header="Pear">
130
+
131
+ Pear types include the **Asian pear** and the **European pear**
132
+ </cv-tab>
133
+ </cv-tabgroup>
134
+ ```
135
+
136
+ * **Synchronized Tab Groups** Multiple tab groups with the same `id` will automatically synchronize. When you switch tabs in one group, all other groups with the same ID will update simultaneously.
137
+
138
+ ### Attributes & Options
139
+
140
+ #### `<cv-tabgroup>` Attributes
141
+
142
+ | Name | Type | Default | Description |
143
+ |------|------|---------|-------------|
144
+ | `id` | string | **(required)** | Unique identifier for the tab group. Tab groups with the same ID will synchronize. |
145
+ | `nav` | string | `"auto"` | Navigation display mode. Use `"auto"` to automatically generate navigation, or `"none"` to hide navigation. |
146
+
147
+ #### `<cv-tab>` Attributes
148
+
149
+ | Name | Type | Default | Description |
150
+ |------|------|---------|-------------|
151
+ | `id` | string | **(required)** | Unique identifier for the tab within its group. |
152
+ | `header` | string | Tab ID | Display label for the tab in the navigation bar. Supports text and font-awesome emojis. |
153
+
154
+ **Config integration**:
155
+ If you declare tab groups in `customviews.config.json` under `config.tabGroups`, they will appear in the widget with friendly labels and default selections. Example:
156
+
157
+ ```json
158
+ {
159
+ "config": {
160
+ "tabGroups": [
161
+ {
162
+ "id": "fruit",
163
+ "label": "Fruit Selection",
164
+ "default": "apple",
165
+ "tabs": [
166
+ { "id": "apple", "label": "Apple" },
167
+ { "id": "orange", "label": "Orange" },
168
+ { "id": "pear", "label": "Pear" }
169
+ ]
170
+ }
171
+ ]
172
+ }
173
+ }
174
+ ```
175
+
176
+ Resolution order of tabs and toggles is as follows:
177
+ * URL state (if present) → persisted localStorage state → `config.defaultState`.
178
+
179
+
180
+ ### Simple Setup (auto-init)
181
+
182
+ To get started quickly, add the following snippet to your site's `<head>` or right before `</body>`:
183
+
184
+ ```html
185
+ <!-- Load from CDN or your hosted bundle -->
186
+ <script src="https://unpkg.com/@customviews-js/customviews/dist/custom-views.min.js"
187
+ data-base-url="/customviews"
188
+ ></script>
189
+ ```
190
+
191
+ This snippet loads:
192
+ * The CustomViews runtime, a lightweight JavaScript that auto-initializes on page load.
193
+ * The base URL and config path, which tell the runtime where to find your assets and configuration.
194
+
195
+ By default the auto-init script will:
196
+ 1. Load configuration (default path `/customviews.config.json`)
197
+ 2. Initialize the core library
198
+ 3. Create and render the widget if enabled
199
+
200
+ ## Configuration
201
+
202
+ The config file uses a flat top-level structure. Key fields:
203
+
204
+ Example `customviews.config.json`:
205
+
206
+ ```json
207
+ {
208
+ "config": {
209
+ "allToggles": ["mac", "linux", "windows"],
210
+ "defaultState": {
211
+ "toggles": ["mac", "linux", "windows"],
212
+ "tabs": { "fruit": "apple" }
213
+ },
214
+ "tabGroups": [
215
+ {
216
+ "id": "fruit",
217
+ "label": "Fruit Selection",
218
+ "default": "apple",
219
+ "tabs": [
220
+ { "id": "apple", "label": "Apple" },
221
+ { "id": "orange", "label": "Orange" },
222
+ { "id": "pear", "label": "Pear" }
223
+ ]
224
+ }
225
+ ]
226
+ },
227
+ "assetsJsonPath": "/assets/assets.json",
228
+ "baseUrl": "/customviews",
229
+ "showUrl": false,
230
+ "widget": {
231
+ "enabled": true,
232
+ "position": "middle-left",
233
+ "showReset": true,
234
+ "title": "Customize your view here",
235
+ "description": "Toggle content sections and switch between different tab views.",
236
+ "showWelcome": true
237
+ }
238
+ }
239
+ ```
240
+
241
+ #### Key Fields
242
+
243
+ | Field | Description |
244
+ | ------ | ------------ |
245
+ | `config.allToggles` | List of toggle IDs recognized by CustomViews. |
246
+ | `config.defaultState` | Toggles and tabs per tab group enabled by default on first load. |
247
+ | `assetsJsonPath` | Path to your compiled assets manifest (resolved relative to `baseURL`). |
248
+ | `widget` | Floating widget settings — control position, visibility, and welcome prompt. |
249
+ | `showUrl` | When true, CustomViews encodes the active state into the browser URL for shareable links. |
250
+
251
+ #### Script Attributes
252
+
253
+ The auto-init script supports these data attributes:
254
+ - `data-base-url`: Base URL for all paths (e.g., `/customviews`)
255
+ - `data-config-path`: Custom path to config file (default: `/customviews.config.json`)
256
+
257
+ ```html
258
+ <script
259
+ src="https://unpkg.com/@customviews-js/customviews"
260
+ data-base-url="/my-site"
261
+ data-config-path="/configs/customviews.config.json">
262
+ </script>
263
+ ```
264
+
265
+
266
+ ## Widget Features
267
+
268
+ - Floating widget with optional welcome modal and reset
269
+ - Config-driven (enable/disable, position, theme, labels)
270
+ - Shows toggles and tab groups declared in `config` for quick switching
271
+ - Can persist state to URL for sharing
272
+
273
+ Widget configuration options:
274
+
275
+ | Name | Type | Default | Description |
276
+ |------|------|---------|-------------|
277
+ | `enabled` | boolean | true | Whether to show the widget. |
278
+ | `position` | string | 'middle-left' | Position of the widget (e.g., 'middle-left', 'bottom-right'). |
279
+ | `showReset` | boolean | true | Whether to show the reset button. |
280
+ | `showWelcome` | boolean | false | Whether to show a welcome modal on first visit. |
281
+
282
+ ```typescript
283
+ {
284
+ core: CustomViewsCore; // Required: core instance
285
+ position?: string; // Default: 'middle-left'
286
+ theme?: 'light' | 'dark'; // Default: 'light'
287
+ showReset?: boolean; // Default: true
288
+ showWelcome?: boolean; // Default: false
289
+ welcomeTitle?: string; // Custom welcome title
290
+ welcomeMessage?: string; // Custom welcome message
291
+ title?: string; // Widget tooltip
292
+ description?: string; // Widget modal description
293
+ }
294
+ ```
295
+
296
+ ## Assets JSON
297
+
298
+ Assets are still defined in a JSON file referenced by `assetsJsonPath`. Example snippets are unchanged — asset keys map to HTML, text, or images and are rendered into elements that have `data-cv-id` (or legacy `data-customviews-id`).
299
+
300
+ The `assets.json` file defines reusable content that can be dynamically injected into your pages. Each asset is referenced by a unique key and can contain images, HTML, or text.
301
+ * Note that `baseURL` defined during initialization is automatically prepended to all asset `src` paths.
302
+
303
+ ### Structure
304
+
305
+ ```json
306
+ {
307
+ "htmlContent": {
308
+ "content": "<h2>Dynamic HTML</h2><p>This content is injected dynamically.</p>"
309
+ },
310
+ "textContent": {
311
+ "content": "Plain text content"
312
+ },
313
+ "imageContent": {
314
+ "src": "/assets/mac.png"
315
+ },
316
+ "assetKey": {
317
+ "src": "/path/to/image.png",
318
+ "alt": "Description",
319
+ "className": "custom-class",
320
+ "style": "border-radius: 8px;"
321
+ },
322
+ }
323
+ ```
324
+
325
+ ### Usage in HTML
326
+
327
+ ```html
328
+ <!-- Reference asset by key using data-cv-id (or data-customviews-id for backward compatibility) -->
329
+ <div data-cv-toggle="beginner" data-cv-id="assetKey"></div>
330
+ ```
331
+
332
+ When the toggle is active, the asset will be automatically rendered into the element.
333
+
334
+
335
+ ## Troubleshooting
336
+
337
+ * Toggles not appearing in widget?
338
+ * Check `config.allToggles` includes your toggle IDs.
339
+
340
+ * No effect when toggling?
341
+ * Ensure the element uses `data-cv-toggle` or `<cv-toggle>` with `category`, and matches an active toggle ID.
342
+
343
+ * URL state not persisting in URL bar?
344
+ * Enable `showUrl` in the configuration.
345
+
346
+ * Widget not loading?
347
+ * Verify the script is included and `customviews.config.json` is accessible.
348
+
349
+ * Tab groups not synchronizing?
350
+ * Ensure they share the same `id` attribute.
351
+
352
+ * Assets not rendering?
353
+ * Check `data-cv-id` matches a key in `assets.json`, and the toggle is active.
354
+
355
+ ## API & Integration Notes
356
+
357
+ - Public globals: `window.CustomViews`, `window.CustomViewsWidget`, and `window.customViewsInstance` (contains `{ core, widget? }`) when auto-initialized.
358
+ - Important events: `customviews:ready` (auto-init done) and `customviews:tab-change` (dispatched when a tab group selection changes).
359
+ - URL state helpers: `URLStateManager.generateShareableURL` and `URLStateManager.parseURL` (used internally by the widget).
360
+
361
+ ## License
362
+
220
363
  MIT