@scalar/nuxt 0.3.33 → 0.4.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/dist/module.d.mts CHANGED
@@ -22,6 +22,7 @@ type ModuleOptions = {
22
22
  * configurations. These configurations will extend over the base config
23
23
  */
24
24
  configurations: Omit<Configuration, 'devtools'>[];
25
+ layout: string | false;
25
26
  } & Configuration;
26
27
  declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
27
28
 
package/dist/module.d.ts CHANGED
@@ -22,6 +22,7 @@ type ModuleOptions = {
22
22
  * configurations. These configurations will extend over the base config
23
23
  */
24
24
  configurations: Omit<Configuration, 'devtools'>[];
25
+ layout: string | false;
25
26
  } & Configuration;
26
27
  declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
27
28
 
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@scalar/nuxt",
3
3
  "configKey": "scalar",
4
- "version": "0.3.33",
4
+ "version": "0.4.2",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "0.8.1",
7
7
  "unbuild": "unknown"
package/dist/module.mjs CHANGED
@@ -16,7 +16,8 @@ const module = defineNuxtModule({
16
16
  },
17
17
  showSidebar: true,
18
18
  devtools: true,
19
- configurations: []
19
+ configurations: [],
20
+ layout: false
20
21
  },
21
22
  setup(_options, _nuxt) {
22
23
  const resolver = createResolver(import.meta.url);
@@ -57,6 +58,7 @@ const module = defineNuxtModule({
57
58
  name: "scalar-" + index,
58
59
  path: configuration.pathRouting?.basePath + ":pathMatch(.*)*",
59
60
  meta: {
61
+ layout: _options.layout,
60
62
  configuration,
61
63
  isOpenApiEnabled
62
64
  },
@@ -68,6 +70,7 @@ const module = defineNuxtModule({
68
70
  name: "scalar",
69
71
  path: _options.pathRouting?.basePath + ":pathMatch(.*)*",
70
72
  meta: {
73
+ layout: _options.layout,
71
74
  configuration: _options,
72
75
  isOpenApiEnabled
73
76
  },
@@ -1,15 +1,22 @@
1
1
  <script lang="ts" setup>
2
2
  import { ModernLayout, parse } from '@scalar/api-reference'
3
3
  import type { ApiReferenceConfiguration } from '@scalar/types/api-reference'
4
- import { useHead, useRequestURL, useSeoMeta } from '#imports'
4
+ import { useColorMode } from '@scalar/use-hooks/useColorMode'
5
+ import { useFetch, useHead, useRequestURL, useSeoMeta } from '#imports'
5
6
  import type { Configuration } from '~/src/types'
6
- import { reactive, ref, toRaw } from 'vue'
7
+ import { onMounted, reactive, ref, toRaw, watch } from 'vue'
7
8
 
8
9
  const props = defineProps<{
9
10
  configuration: Configuration
10
11
  }>()
11
12
 
12
13
  const isDark = ref(props.configuration.darkMode)
14
+ const forcedMode = props.configuration.forceDarkModeState
15
+
16
+ const { colorMode, toggleColorMode } = useColorMode({
17
+ initialColorMode: props.configuration.darkMode ? 'dark' : 'light',
18
+ overrideColorMode: props.configuration.forceDarkModeState,
19
+ })
13
20
 
14
21
  // @ts-expect-error support the old syntax for a bit
15
22
  const content = props.configuration.spec?.content ?? props.configuration.content
@@ -23,8 +30,9 @@ const document =
23
30
  : content
24
31
  ? toRaw(content)
25
32
  : url
26
- ? await $fetch<string>(url)
27
- : await $fetch<string>('/_openapi.json')
33
+ ? (await useFetch<string>(url, { responseType: 'text' })).data.value
34
+ : (await useFetch<string>('/_openapi.json', { responseType: 'text' }))
35
+ .data.value
28
36
 
29
37
  // Check for empty spec
30
38
  if (!document) {
@@ -40,9 +48,34 @@ if (props.configuration?.metaData) {
40
48
  }
41
49
 
42
50
  useHead({
43
- bodyAttrs: {
44
- class: () => (isDark.value ? 'dark-mode' : 'light-mode'),
45
- },
51
+ script: [
52
+ {
53
+ // Inject dark / light detection that runs before loading Nuxt to avoid flicker
54
+ // This is a bit of a hack inspired by @nuxtjs/color-mode, but it works
55
+ id: 'scalar-color-mode-script',
56
+ tagPosition: 'bodyClose',
57
+ innerHTML: `((isDark, forced) => {
58
+ try {
59
+ const stored = window.localStorage.getItem('colorMode');
60
+ const useDark = forced === 'dark' || !forced && (stored === 'dark' || !stored && isDark);
61
+ window.document.body.classList.add(useDark ? 'dark-mode' : 'light-mode');
62
+ } catch {}
63
+ })(${isDark.value}, ${JSON.stringify(forcedMode)});`
64
+ .replace(/[\n\r]/g, '')
65
+ .replace(/ +/g, ' '),
66
+ },
67
+ ],
68
+ })
69
+
70
+ watch(colorMode, () => {
71
+ isDark.value = colorMode.value === 'dark'
72
+ })
73
+
74
+ onMounted(() => {
75
+ // Adjust the color mode toggle switch
76
+ isDark.value = window.document.body.classList.contains('dark-mode')
77
+ // Remove scalar-color-mode-script
78
+ window.document.getElementById('scalar-color-mode-script')?.remove()
46
79
  })
47
80
 
48
81
  // Add baseServerURL and _integration
@@ -61,7 +94,7 @@ const config: Partial<ApiReferenceConfiguration> = {
61
94
  :isDark="!!isDark"
62
95
  :parsedSpec="parsedSpec"
63
96
  :rawSpec="rawSpec"
64
- @toggleDarkMode="isDark = !isDark" />
97
+ @toggleDarkMode="() => toggleColorMode()" />
65
98
  </template>
66
99
 
67
100
  <style>
package/package.json CHANGED
@@ -20,9 +20,9 @@
20
20
  "testing",
21
21
  "vue"
22
22
  ],
23
- "version": "0.3.33",
23
+ "version": "0.4.2",
24
24
  "engines": {
25
- "node": ">=18"
25
+ "node": ">=20"
26
26
  },
27
27
  "type": "module",
28
28
  "main": "./dist/module.cjs",
@@ -39,9 +39,10 @@
39
39
  ],
40
40
  "dependencies": {
41
41
  "@nuxt/kit": "^3.12.3",
42
- "@scalar/api-client": "2.3.33",
43
- "@scalar/types": "0.1.15",
44
- "@scalar/api-reference": "1.28.33"
42
+ "@scalar/api-client": "2.4.2",
43
+ "@scalar/api-reference": "1.29.2",
44
+ "@scalar/use-hooks": "0.2.1",
45
+ "@scalar/types": "0.2.1"
45
46
  },
46
47
  "devDependencies": {
47
48
  "@nuxt/devtools": "^1.3.9",
@@ -51,11 +52,12 @@
51
52
  "@nuxt/test-utils": "^3.13.1",
52
53
  "@types/node": "^20.17.10",
53
54
  "changelogen": "^0.5.5",
55
+ "cross-env": "^7.0.3",
54
56
  "nuxt": "^3.12.4",
55
57
  "vitest": "^1.6.0"
56
58
  },
57
59
  "scripts": {
58
- "build": "NUXT_TELEMETRY_DISABLED=false pnpm dev:prepare && nuxt-module-build build",
60
+ "build": "cross-env NUXT_TELEMETRY_DISABLED=false pnpm dev:prepare && nuxt-module-build build",
59
61
  "dev": "pnpm dev:prepare && nuxi dev playground",
60
62
  "dev:build": "nuxi build playground",
61
63
  "dev:prepare": "nuxi prepare && nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground",