@csedl/hotwire-svelte-helpers 2.2.0 → 2.2.2
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
|
@@ -6,7 +6,7 @@ Dropdown with stimulus, based on floating-UI.
|
|
|
6
6
|
|
|
7
7
|
**Links:**
|
|
8
8
|
- [Online Demo App](https://hotwire-svelte-helpers.sedlmair.ch/)
|
|
9
|
-
- [Ruby Gem:
|
|
9
|
+
- [Ruby Gem: stimulus-overlay-helpers](https://gitlab.com/sedl/stimulus-overlay-helpers)
|
|
10
10
|
- [How we are building a Rails App](https://dev.to/chmich/setup-vite-svelte-inertia-stimulus-bootstrap-foundation-on-rails-7-overview-1bk1)
|
|
11
11
|
|
|
12
12
|
## Setup
|
|
@@ -19,13 +19,19 @@ HotwireSvelteHelpers.initializeOverlays()
|
|
|
19
19
|
|
|
20
20
|
This package has a Server-Side Rendering (SSR) safe configuration, which means:
|
|
21
21
|
|
|
22
|
-
You must add the file `<vite-sourcee-code-dir>/config/hotwire-svelte-helpers.js
|
|
22
|
+
You must add the file `<vite-sourcee-code-dir>/config/hotwire-svelte-helpers.js`.
|
|
23
|
+
The package inside does a fixed `import customConfigs from '/config/hotwire-svelte-helpers.js'`.
|
|
24
|
+
By this, it must find that file.
|
|
25
|
+
|
|
26
|
+
Minimal content of this file:
|
|
23
27
|
|
|
24
28
|
```javascript
|
|
25
29
|
export default {
|
|
26
30
|
}
|
|
27
31
|
```
|
|
28
32
|
|
|
33
|
+
## Configuration
|
|
34
|
+
|
|
29
35
|
The package-internal default configs, which can be overwritten hereby, are:
|
|
30
36
|
|
|
31
37
|
```javascript
|
|
@@ -42,9 +48,15 @@ export default {
|
|
|
42
48
|
// this may mostly be helpful for development, so you may make it environment-dependent
|
|
43
49
|
closeButtonSvg: null,
|
|
44
50
|
// raw import of svg-icon,
|
|
51
|
+
|
|
52
|
+
formFieldWrapperClass: 'form-field-wrapper',
|
|
53
|
+
formFieldLabelWrapperClass: 'form-field-label-wrapper',
|
|
54
|
+
formFieldInputWrapperClass: 'form-field-input-wrapper',
|
|
45
55
|
}
|
|
46
56
|
```
|
|
47
57
|
|
|
58
|
+
The code you can find in this package under `src/lib/configs.js`
|
|
59
|
+
|
|
48
60
|
You can add as many keys as you like and fetch them from anywhere by:
|
|
49
61
|
|
|
50
62
|
```javascript
|
|
@@ -52,9 +64,28 @@ import { hotwireSvelteConfig } from "@csedl/hotwire-svelte-helpers";
|
|
|
52
64
|
hotwireSvelteConfig('closeButtonSelector')
|
|
53
65
|
```
|
|
54
66
|
|
|
55
|
-
##
|
|
67
|
+
## cleanMount()
|
|
68
|
+
|
|
69
|
+
Interaction with (Svelte) components can lead to orphaned instances.
|
|
70
|
+
This can happen when the [unmount](https://svelte.dev/docs/svelte/imperative-component-api#unmount) event does not happen, for example when the underlaying DOM element disappears which can happen in a Hotwire Environemnt.
|
|
71
|
+
|
|
72
|
+
For this here is a double security. CleanMount() adds the instance to a global store and then executes mount().
|
|
73
|
+
|
|
74
|
+
```javascript
|
|
75
|
+
import {cleanMount} from "@csedl/hotwire-svelte-helpers";
|
|
76
|
+
cleanMount(AnySvelteComponent, { target: cleanMountDemoTag, ...});
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Next, you must add something like
|
|
56
80
|
|
|
57
|
-
|
|
81
|
+
```javascript
|
|
82
|
+
import { unmountAllDetached } from '@csedl/hotwire-svelte-helpers'
|
|
83
|
+
document.addEventListener('turbo:render', () => {
|
|
84
|
+
unmountAllDetached()
|
|
85
|
+
})
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
to your application.js. This will check for detached instances and unmount them.
|
|
58
89
|
|
|
59
90
|
## Dropdown Example
|
|
60
91
|
|
|
@@ -93,11 +124,6 @@ This behaviour can be stopped on:
|
|
|
93
124
|
|
|
94
125
|
The functions of this package are intended to give flexibility on various ways building a dropdownSvelte.
|
|
95
126
|
|
|
96
|
-
- Stimulus Controller with Rails Helper
|
|
97
|
-
- Example: [Stimulus controller within this package](https://gitlab.com/sedl/csedl-stimulus-dropdown-js/-/blob/main/src/dropdown-controller.js?ref_type=heads)
|
|
98
|
-
- Stimulus with Svelte component
|
|
99
|
-
- Example: [Stimulus controller on example app](https://gitlab.com/sedl/stimulusfloatingdropdown/-/blob/main/app/frontend/javascript/svelte-dropdown-controller.js?ref_type=heads)
|
|
100
|
-
|
|
101
127
|
**Important:** When creating or initializing the dropdownSvelte,
|
|
102
128
|
always call the initialize function before attaching a listener to the panel's close event.
|
|
103
129
|
The initialize function adds a `close` event listener to the panel that executes the `onPanelClose` function.
|
package/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
|
|
2
2
|
import { cleanMount, unmountAllDetached } from './src/svelte/cleanMount.js';
|
|
3
|
-
import { initializeDropdown, openDropdownPanel, getOrSetPanelId, debugLog,
|
|
3
|
+
import { initializeDropdown, openDropdownPanel, getOrSetPanelId, debugLog, getCsrfToken } from './src/lib/utils.js'
|
|
4
|
+
import { hotwireSvelteConfig } from './src/lib/config.js'
|
|
4
5
|
|
|
5
6
|
export {
|
|
6
7
|
cleanMount, unmountAllDetached, hotwireSvelteConfig,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@csedl/hotwire-svelte-helpers",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.2",
|
|
4
4
|
"description": "Hotwire + Svelte helpers for Rails: Stimulus floating dropdowns/toolips + Svelte global panels/modals + RTurbo-friendly utilities. Build together with the rubygem svelte-on-rails and its npm-package.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import customConfigs from '/config/hotwire-svelte-helpers.js'
|
|
2
|
+
|
|
3
|
+
export function hotwireSvelteConfig(key) {
|
|
4
|
+
|
|
5
|
+
const defaultConfig = {
|
|
6
|
+
closeButtonSelector: '.close-button',
|
|
7
|
+
dropdownContentSelector: '.content',
|
|
8
|
+
tooltipContentSelector: '.content',
|
|
9
|
+
addArrow: true,
|
|
10
|
+
persistTooltipOnClick: false,
|
|
11
|
+
closeButtonSvg: '',
|
|
12
|
+
|
|
13
|
+
formFieldWrapperClass: 'form-field-wrapper',
|
|
14
|
+
formFieldLabelWrapperClass: 'form-field-label-wrapper',
|
|
15
|
+
formFieldInputWrapperClass: 'form-field-input-wrapper',
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const configs = {...defaultConfig, ...customConfigs}
|
|
19
|
+
|
|
20
|
+
if (!Object.keys(configs).includes(key)) {
|
|
21
|
+
throw new Error(`[@csedl/hotwire-svelte-helpers] unknown config key: ${key}, actual keys are:\n • ${Object.keys(configs).join("\n • ")}`)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return configs[key] || null
|
|
25
|
+
}
|
package/src/lib/utils.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import {positionPanelSelf, positionPanelByButton, positionPanel} from "./floating-ui-functions.js";
|
|
2
|
-
import
|
|
3
|
-
import {validateOptions} from "./type-validators";
|
|
2
|
+
import {hotwireSvelteConfig} from "./config.js";
|
|
4
3
|
|
|
5
4
|
// Fetch content from server based on panel's data-src attribute and update content
|
|
6
5
|
export function openDropdownPanel(buttonElement, panelElement, options = {}) {
|
|
@@ -9,9 +8,6 @@ export function openDropdownPanel(buttonElement, panelElement, options = {}) {
|
|
|
9
8
|
return
|
|
10
9
|
}
|
|
11
10
|
|
|
12
|
-
//validateOptions(options, {clickedElement: 'object'})
|
|
13
|
-
//const clickedElement = options.clickedElement || buttonElement;
|
|
14
|
-
|
|
15
11
|
|
|
16
12
|
// link button and panel, initialize
|
|
17
13
|
panelElement.id = getOrSetPanelId(buttonElement);
|
|
@@ -267,26 +263,6 @@ export function getOrSetPanelId(buttonTag) {
|
|
|
267
263
|
}
|
|
268
264
|
}
|
|
269
265
|
|
|
270
|
-
export function hotwireSvelteConfig(key) {
|
|
271
|
-
|
|
272
|
-
const defaultConfig = {
|
|
273
|
-
closeButtonSelector: '.close-button',
|
|
274
|
-
dropdownContentSelector: '.content',
|
|
275
|
-
tooltipContentSelector: '.content',
|
|
276
|
-
addArrow: true,
|
|
277
|
-
persistTooltipOnClick: false,
|
|
278
|
-
closeButtonSvg: '',
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
const configs = {...defaultConfig, ...customConfigs}
|
|
282
|
-
|
|
283
|
-
if (!Object.keys(configs).includes(key)) {
|
|
284
|
-
throw new Error(`[@csedl/hotwire-svelte-helpers] unknown config key: ${key}, actual keys are:\n • ${Object.keys(configs).join("\n • ")}`)
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
return configs[key] || null
|
|
288
|
-
}
|
|
289
|
-
|
|
290
266
|
export function getCsrfToken() {
|
|
291
267
|
return document.querySelector('meta[name="csrf-token"]')?.getAttribute('content') || '';
|
|
292
268
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {Controller} from "@hotwired/stimulus"
|
|
2
2
|
import {positionPanelByButton} from "../lib/floating-ui-functions.js";
|
|
3
|
-
import {
|
|
3
|
+
import { debugLog, findPanelOrThrow, openDropdownPanel} from "../lib/utils.js";
|
|
4
|
+
import {hotwireSvelteConfig} from "../lib/config.js";
|
|
4
5
|
|
|
5
6
|
export default class extends Controller {
|
|
6
7
|
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
<script>
|
|
2
2
|
|
|
3
|
+
import { hotwireSvelteConfig } from "../../lib/config.js";
|
|
4
|
+
|
|
3
5
|
let {
|
|
4
6
|
context,
|
|
5
7
|
columnName,
|
|
@@ -45,11 +47,11 @@
|
|
|
45
47
|
</script>
|
|
46
48
|
|
|
47
49
|
|
|
48
|
-
<div class="
|
|
49
|
-
<div class=
|
|
50
|
-
<label
|
|
50
|
+
<div class="{hotwireSvelteConfig('formFieldWrapperClass')} {model}_{columnName} {inputType} {fieldErrorsClass}">
|
|
51
|
+
<div class={hotwireSvelteConfig('formFieldLabelWrapperClass')}>
|
|
52
|
+
<label for="{model}_{columnName}">{label()}</label>
|
|
51
53
|
</div>
|
|
52
|
-
<div class=
|
|
54
|
+
<div class={hotwireSvelteConfig('formFieldInputWrapperClass')}>
|
|
53
55
|
|
|
54
56
|
{#if columnSchema.enum}
|
|
55
57
|
<select
|