@forsakringskassan/docs-live-example 1.3.0

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/LICENSE.md ADDED
@@ -0,0 +1,24 @@
1
+ # The MIT License (MIT)
2
+
3
+ Copyright © 2024 Försäkringskassan
4
+
5
+ Permission is hereby granted, free of charge, to any person
6
+ obtaining a copy of this software and associated documentation
7
+ files (the “Software”), to deal in the Software without
8
+ restriction, including without limitation the rights to use,
9
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the
11
+ Software is furnished to do so, subject to the following
12
+ conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24
+ OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,191 @@
1
+ # `@forsakringskassan/docs-live-example`
2
+
3
+ `live-example` är en Vue-komponent som används för att presentera ett levande exempel samt tillhörande markup för att reproducera exemplet.
4
+
5
+ Komponenten består av tre ytor:
6
+
7
+ - exempelyta: innehåller det kompilerade exemplet.
8
+ - kontrollyta: innehåller de inmatningsfält som används för att konfigurera exemplet.
9
+ - kodyta: visar HTML-markup och Vue-template för exemplet (om Vue-komponenter används).
10
+
11
+ Eftersom `live-example` kompileras i runtime så kan man direkt modifiera exemplet och den markup som visas genom att använda de inmatningsfält som lagts till i kontrollytan.
12
+
13
+ ## Användning
14
+
15
+ Installera paketet genom att köra:
16
+
17
+ `npm install --save-dev @forsakringskassan/docs-live-example`
18
+
19
+ Om du använder `html-validate` bör du även uppdatera din `.htmlvalidate.json` med följande rader för att registrera `live-example` elementet:
20
+
21
+ ```json
22
+ "extends": [
23
+ "@forsakringskassan/docs-live-example/htmlvalidate:recommended",
24
+ ],
25
+ "plugins": [
26
+ "@forsakringskassan/docs-live-example/htmlvalidate"
27
+ ],
28
+ ```
29
+
30
+ ### Props
31
+
32
+ #### `template`
33
+
34
+ För att generera ett exempel så används `template` för att skicka in markup.
35
+
36
+ #### `components` (optional)
37
+
38
+ De Vue-komponenter som används i markup som skickas in till `template` måste läggas till i `components`.
39
+
40
+ #### `livedata` (optional)
41
+
42
+ Om exemplet behöver spara ett värde (till exempel `v-model`) så skickas detta in genom `livedata`.
43
+
44
+ #### `livemethods` (optional)
45
+
46
+ Om exemplet behöver köra en metod så skickas detta in genom `livemethods`.
47
+
48
+ ### Konfigurera exemplet
49
+
50
+ För att skapa ett konfigurerbart exempel börjar vi med att skapa en ny komponent `AwesomeComponentLiveExample.vue`.
51
+ Vi rekommenderar att använda `LiveExample` som suffix på alla live-exempel.
52
+
53
+ Följande boilerplate kan användas:
54
+
55
+ ```vue
56
+ <template>
57
+ <live-example :components :template :livedata>
58
+ <!-- Example configuration -->
59
+ </live-example>
60
+ </template>
61
+
62
+ <script lang="ts">
63
+ import { defineComponent } from "vue";
64
+ import { LiveExample } from "@forsakringskassan/docs-live-example";
65
+
66
+ export default defineComponent({
67
+ name: "AwesomeComponentLiveExample",
68
+ components: { LiveExample },
69
+ data() {
70
+ return {};
71
+ },
72
+ computed: {
73
+ livedata(): unknown {
74
+ return {
75
+ /* data used by generated code */
76
+ };
77
+ },
78
+ components(): unknown {
79
+ return {
80
+ /* components used by generated code */
81
+ };
82
+ },
83
+ template(): string {
84
+ return /* HTML */ ` <div>Hello World!</div> `;
85
+ },
86
+ },
87
+ });
88
+ </script>
89
+ ```
90
+
91
+ För att skapa en inställning lägger vi först in komponenter:
92
+
93
+ ```diff
94
+ <live-example :components :template :livedata>
95
+ + <f-select-field v-model="tagName">
96
+ + <template #label> Element </template>
97
+ + <option value="div"> div </option>
98
+ + <option value="p"> p </option>
99
+ + <option value="em"> em </option>
100
+ + </f-select-field>
101
+ + <f-checkbox-field v-model="placeholderText" :value="true">
102
+ + Use placeholder text
103
+ + </f-checkbox-field>
104
+ </live-example>
105
+ ```
106
+
107
+ ```diff
108
+ data() {
109
+ - return {};
110
+ + return {
111
+ + tagName: "div",
112
+ + placeholderText: false,
113
+ + };
114
+ },
115
+ ```
116
+
117
+ Därefter kan vi modifiera `template` att nyttja inställningar:
118
+
119
+ ```diff
120
+ template(): string {
121
+ - return /* HTML */ ` <div>Hello World!</div> `;
122
+ + const { tagName, placeholderText } = this;
123
+ + const message = placeholderText ? "Lorem ipsum dolor sit amet" : "Hello World!" ;
124
+ + return /* HTML */ ` <${tagName}>${message}</${tagName}> `;
125
+ },
126
+ ```
127
+
128
+ Det går också med fördel att använda `createElement` (se beskrivning längre ner):
129
+
130
+ ```diff
131
+ template(): string {
132
+ - return /* HTML */ ` <div>Hello World!</div> `;
133
+ + const { tagName, placeholderText } = this;
134
+ + const message = placeholderText ? "Lorem ipsum dolor sit amet" : "Hello World!" ;
135
+ + return createElement(tagName, message);
136
+ },
137
+ ```
138
+
139
+ ## `createElement`
140
+
141
+ A helper function to render the markup for the live example.
142
+
143
+ ```ts
144
+ createElement(tagName);
145
+ createElement(tagName, content);
146
+ createElement(tagName, attributes);
147
+ createElement(tagName, attributes, content);
148
+ ```
149
+
150
+ Create markup for a simple element:
151
+
152
+ ```ts
153
+ createElement("div");
154
+ // <div>
155
+ ```
156
+
157
+ Adding attributes:
158
+
159
+ ```ts
160
+ createElement("div", { id: "my-awesome-id", class: ["foo", "bar"] });
161
+ // <div id="my-awesome-id" class="foo bar">
162
+ ```
163
+
164
+ Attributes can be:
165
+
166
+ - `string` - value is passed as-is: `{ key: "value" }` becomes `key="value"`.
167
+ - `number` - value is converted to string: `{ key: 12 }` becomes `key="12"`.
168
+ - `boolean` - key is set if value is true: `{ key: true }` becomes `key` and `{ key: false }` omits the attribute.
169
+ - `Array` - each non-empty item is joined: `{ key: ["foo", "bar"] }` becomes `key="foo bar"`.
170
+ - `Object` - nests attributes: `{ data: { key: "value" } }` becomes `data-key="value"`.
171
+ - `null` and `undefined` are omitted from its context, e.g. `{ key: null }` `{ key: [null] ` and `{ key: { value: null } }` are all omitted.
172
+
173
+ Content can be added:
174
+
175
+ ```ts
176
+ createElement("div", "lorem ipsum");
177
+ // <div> lorem ipsum </div>
178
+
179
+ createElement("div", [
180
+ createElement("h1", "My Awesome Heading"),
181
+ createElement("p", ["Lorem ipsum", "dolor sit amet"]),
182
+ ]);
183
+ // <div> <h1> My Awesome Heading </h1> <p> Lorem ipsum dolor sit amet </p> </div>
184
+ ```
185
+
186
+ Combined:
187
+
188
+ ```ts
189
+ createElement("div", { id: "foo" }, "lorem ipsum");
190
+ // <div id="foo"> lorem ipsum </div>
191
+ ```
@@ -0,0 +1,36 @@
1
+ declare const _default: import("vue").DefineComponent<{
2
+ template: {
3
+ type: StringConstructor;
4
+ required: true;
5
+ };
6
+ components: {
7
+ type: ObjectConstructor;
8
+ required: true;
9
+ };
10
+ livedata: {
11
+ type: ObjectConstructor;
12
+ required: true;
13
+ };
14
+ livemethods: {
15
+ type: ObjectConstructor;
16
+ required: true;
17
+ };
18
+ }, unknown, unknown, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, Readonly<import("vue").ExtractPropTypes<{
19
+ template: {
20
+ type: StringConstructor;
21
+ required: true;
22
+ };
23
+ components: {
24
+ type: ObjectConstructor;
25
+ required: true;
26
+ };
27
+ livedata: {
28
+ type: ObjectConstructor;
29
+ required: true;
30
+ };
31
+ livemethods: {
32
+ type: ObjectConstructor;
33
+ required: true;
34
+ };
35
+ }>>, {}, {}>;
36
+ export default _default;
@@ -0,0 +1,62 @@
1
+ import { type ExpandAnimation } from "./expand-animation";
2
+ declare const _default: import("vue").DefineComponent<{
3
+ template: {
4
+ type: StringConstructor;
5
+ required: true;
6
+ };
7
+ components: {
8
+ type: ObjectConstructor;
9
+ required: false;
10
+ default: () => {};
11
+ };
12
+ livedata: {
13
+ type: ObjectConstructor;
14
+ required: false;
15
+ default: () => {};
16
+ };
17
+ livemethods: {
18
+ type: ObjectConstructor;
19
+ required: false;
20
+ default: () => {};
21
+ };
22
+ }, unknown, {
23
+ idPrefix: string;
24
+ codeLanguage: string;
25
+ codeExpand: ExpandAnimation;
26
+ }, {
27
+ isVue(): boolean;
28
+ codeToggleText(): string;
29
+ exampleElement(): HTMLElement;
30
+ expandableElement(): HTMLElement;
31
+ templateElement(): HTMLElement;
32
+ }, {
33
+ onToggleCode(): void;
34
+ compileCode(): void;
35
+ compileVue(): Promise<void>;
36
+ compileHTML(): Promise<void>;
37
+ }, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, Readonly<import("vue").ExtractPropTypes<{
38
+ template: {
39
+ type: StringConstructor;
40
+ required: true;
41
+ };
42
+ components: {
43
+ type: ObjectConstructor;
44
+ required: false;
45
+ default: () => {};
46
+ };
47
+ livedata: {
48
+ type: ObjectConstructor;
49
+ required: false;
50
+ default: () => {};
51
+ };
52
+ livemethods: {
53
+ type: ObjectConstructor;
54
+ required: false;
55
+ default: () => {};
56
+ };
57
+ }>>, {
58
+ components: Record<string, any>;
59
+ livedata: Record<string, any>;
60
+ livemethods: Record<string, any>;
61
+ }, {}>;
62
+ export default _default;
@@ -0,0 +1,9 @@
1
+ export type PlainValue = string | number | boolean | null | undefined;
2
+ export interface Attributes {
3
+ [key: string]: PlainValue | PlainValue[] | Attributes;
4
+ }
5
+ export type Children = string | string[];
6
+ export declare function createElement(tagName: string): string;
7
+ export declare function createElement(tagName: string, children: Children): string;
8
+ export declare function createElement(tagName: string, attrs: Attributes): string;
9
+ export declare function createElement(tagName: string, attrs: Attributes, children: Children): string;
@@ -0,0 +1,5 @@
1
+ export interface ExpandAnimation {
2
+ readonly isOpen: boolean;
3
+ toggle(): void;
4
+ }
5
+ export declare function expandAnimation(element: HTMLElement): ExpandAnimation;
package/dist/index.css ADDED
@@ -0,0 +1,7 @@
1
+ /* sfc-style:/home/runner/work/docs-live-example/docs-live-example/src/LiveExample.vue?type=style&index=0 */
2
+ .collapsed[data-v-f5e7ab48] {
3
+ display: none;
4
+ }
5
+ .collapsed[aria-expanded=true][data-v-f5e7ab48] {
6
+ display: block;
7
+ }
@@ -0,0 +1 @@
1
+ export { default as LiveExample } from "./LiveExample.vue";