@commonpub/layer 0.3.37 → 0.4.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.
@@ -0,0 +1,91 @@
1
+ <script setup lang="ts">
2
+ const { hasConsented, hasNonEssentialCookies, acceptAll, acceptEssential } = useCookieConsent();
3
+
4
+ const visible = computed(() => !hasConsented.value && hasNonEssentialCookies.value);
5
+ </script>
6
+
7
+ <template>
8
+ <Transition name="cpub-consent-slide">
9
+ <div v-if="visible" class="cpub-consent" role="dialog" aria-label="Cookie consent">
10
+ <div class="cpub-consent-inner">
11
+ <p class="cpub-consent-text">
12
+ This site uses cookies for essential functionality and to remember your preferences.
13
+ <NuxtLink to="/cookies" class="cpub-consent-link">Learn more</NuxtLink>
14
+ </p>
15
+ <div class="cpub-consent-actions">
16
+ <button class="cpub-btn cpub-btn-sm" @click="acceptEssential">
17
+ Essential only
18
+ </button>
19
+ <button class="cpub-btn cpub-btn-sm cpub-btn-primary" @click="acceptAll">
20
+ Accept all
21
+ </button>
22
+ </div>
23
+ </div>
24
+ </div>
25
+ </Transition>
26
+ </template>
27
+
28
+ <style scoped>
29
+ .cpub-consent {
30
+ position: fixed;
31
+ bottom: 0;
32
+ left: 0;
33
+ right: 0;
34
+ z-index: var(--z-toast);
35
+ background: var(--surface);
36
+ border-top: var(--border-width-default) solid var(--border);
37
+ box-shadow: 0 -4px 12px rgba(0, 0, 0, 0.1);
38
+ }
39
+
40
+ .cpub-consent-inner {
41
+ max-width: var(--content-max-width);
42
+ margin: 0 auto;
43
+ padding: var(--space-4) var(--space-6);
44
+ display: flex;
45
+ align-items: center;
46
+ justify-content: space-between;
47
+ gap: var(--space-4);
48
+ }
49
+
50
+ .cpub-consent-text {
51
+ font-size: var(--text-sm);
52
+ color: var(--text-dim);
53
+ line-height: var(--leading-snug);
54
+ flex: 1;
55
+ min-width: 0;
56
+ }
57
+
58
+ .cpub-consent-link {
59
+ color: var(--accent);
60
+ text-decoration: underline;
61
+ text-underline-offset: 2px;
62
+ }
63
+
64
+ .cpub-consent-actions {
65
+ display: flex;
66
+ gap: var(--space-2);
67
+ flex-shrink: 0;
68
+ }
69
+
70
+ /* Slide-up transition */
71
+ .cpub-consent-slide-enter-active,
72
+ .cpub-consent-slide-leave-active {
73
+ transition: transform 0.3s ease, opacity 0.3s ease;
74
+ }
75
+
76
+ .cpub-consent-slide-enter-from,
77
+ .cpub-consent-slide-leave-to {
78
+ transform: translateY(100%);
79
+ opacity: 0;
80
+ }
81
+
82
+ @media (max-width: 640px) {
83
+ .cpub-consent-inner {
84
+ flex-direction: column;
85
+ align-items: stretch;
86
+ padding: var(--space-4);
87
+ }
88
+ .cpub-consent-actions { justify-content: stretch; }
89
+ .cpub-consent-actions .cpub-btn { flex: 1; }
90
+ }
91
+ </style>
@@ -0,0 +1,117 @@
1
+ import type { CookieDefinition } from '@commonpub/config';
2
+
3
+ /**
4
+ * Built-in CommonPub cookies. Instance operators add theirs via
5
+ * `cookies` in commonpub.config.ts — those are merged at runtime.
6
+ */
7
+ const BUILTIN_COOKIES: CookieDefinition[] = [
8
+ {
9
+ name: 'better-auth.session_token',
10
+ category: 'essential',
11
+ description: 'Authenticates your login session. HttpOnly and secure.',
12
+ duration: '7 days',
13
+ },
14
+ {
15
+ name: 'cpub-consent',
16
+ category: 'essential',
17
+ description: 'Stores your cookie consent choice.',
18
+ duration: '1 year',
19
+ },
20
+ {
21
+ name: 'cpub-color-scheme',
22
+ category: 'functional',
23
+ description: 'Remembers your light/dark mode preference across visits.',
24
+ duration: '1 year',
25
+ },
26
+ ];
27
+
28
+ export type ConsentLevel = 'all' | 'essential' | null;
29
+
30
+ /**
31
+ * Cookie consent composable.
32
+ *
33
+ * Manages consent state via an essential cookie (`cpub-consent`).
34
+ * Provides the full registry of cookies (built-in + instance-custom)
35
+ * and guards for checking whether a category is allowed.
36
+ */
37
+ export function useCookieConsent(): {
38
+ /** Whether the user has made a consent choice */
39
+ hasConsented: ComputedRef<boolean>;
40
+ /** Current consent level */
41
+ consentLevel: Ref<ConsentLevel>;
42
+ /** Whether functional cookies are allowed */
43
+ allowsFunctional: ComputedRef<boolean>;
44
+ /** Whether analytics cookies are allowed */
45
+ allowsAnalytics: ComputedRef<boolean>;
46
+ /** Accept all cookie categories */
47
+ acceptAll: () => void;
48
+ /** Accept only essential cookies */
49
+ acceptEssential: () => void;
50
+ /** Reset consent (re-shows banner) */
51
+ resetConsent: () => void;
52
+ /** Full cookie registry (built-in + custom) */
53
+ cookies: ComputedRef<CookieDefinition[]>;
54
+ /** Whether the banner has non-essential cookies to ask about */
55
+ hasNonEssentialCookies: ComputedRef<boolean>;
56
+ } {
57
+ const consentCookie = useCookie<string | null>('cpub-consent', {
58
+ maxAge: 31536000,
59
+ path: '/',
60
+ sameSite: 'lax',
61
+ });
62
+
63
+ const consentLevel = computed<ConsentLevel>({
64
+ get: () => {
65
+ const val = consentCookie.value;
66
+ if (val === 'all' || val === 'essential') return val;
67
+ return null;
68
+ },
69
+ set: (v: ConsentLevel) => {
70
+ consentCookie.value = v;
71
+ },
72
+ });
73
+
74
+ const hasConsented = computed(() => consentLevel.value !== null);
75
+ const allowsFunctional = computed(() => consentLevel.value === 'all');
76
+ const allowsAnalytics = computed(() => consentLevel.value === 'all');
77
+
78
+ // Merge built-in cookies with instance-custom cookies from runtime config
79
+ const runtimeConfig = useRuntimeConfig();
80
+ const customCookies = computed<CookieDefinition[]>(() => {
81
+ const raw = (runtimeConfig.public as Record<string, unknown>).instanceCookies;
82
+ return Array.isArray(raw) ? raw as CookieDefinition[] : [];
83
+ });
84
+
85
+ const cookies = computed<CookieDefinition[]>(() => [
86
+ ...BUILTIN_COOKIES,
87
+ ...customCookies.value,
88
+ ]);
89
+
90
+ const hasNonEssentialCookies = computed(() =>
91
+ cookies.value.some((c) => c.category !== 'essential'),
92
+ );
93
+
94
+ function acceptAll(): void {
95
+ consentCookie.value = 'all';
96
+ }
97
+
98
+ function acceptEssential(): void {
99
+ consentCookie.value = 'essential';
100
+ }
101
+
102
+ function resetConsent(): void {
103
+ consentCookie.value = null;
104
+ }
105
+
106
+ return {
107
+ hasConsented,
108
+ consentLevel,
109
+ allowsFunctional,
110
+ allowsAnalytics,
111
+ acceptAll,
112
+ acceptEssential,
113
+ resetConsent,
114
+ cookies,
115
+ hasNonEssentialCookies,
116
+ };
117
+ }
@@ -1,34 +1,55 @@
1
- import { applyThemeToElement, isValidThemeId } from '@commonpub/ui';
2
-
3
- const STORAGE_KEY = 'cpub-theme';
1
+ import { applyThemeToElement } from '@commonpub/ui';
2
+ import { THEME_TO_FAMILY, FAMILY_VARIANTS } from '../utils/themeConfig';
4
3
 
4
+ /**
5
+ * Theme composable — SSR-safe, server-resolved.
6
+ *
7
+ * The admin picks the instance theme (via admin panel). Users only toggle
8
+ * between light and dark mode within that theme's family. The server middleware
9
+ * resolves the correct theme on every request — no theme-selection cookie needed.
10
+ *
11
+ * The dark mode preference cookie (`cpub-color-scheme`) is only persisted
12
+ * when the user has accepted functional cookies via the consent banner.
13
+ */
5
14
  export function useTheme(): {
15
+ /** Current active theme ID (resolved from instance default + dark mode) */
6
16
  themeId: Ref<string>;
7
- setTheme: (id: string) => void;
17
+ /** Whether dark mode is active */
18
+ isDark: Ref<boolean>;
19
+ /** The admin-configured instance theme */
20
+ instanceDefault: Ref<string>;
21
+ /** Toggle dark mode — persists preference in cookie if consent given */
22
+ setDarkMode: (dark: boolean) => void;
8
23
  } {
9
- // Use useState so SSR and client agree on 'base' initially
10
- const themeId = useState('cpub-theme', () => 'base');
24
+ const themeId = useState<string>('cpub-theme', () => 'base');
25
+ const instanceDefault = useState<string>('cpub-instance-theme', () => 'base');
26
+ const isDark = useState<boolean>('cpub-dark-mode', () => false);
27
+ const schemeCookie = useCookie('cpub-color-scheme', {
28
+ maxAge: 31536000,
29
+ path: '/',
30
+ sameSite: 'lax',
31
+ });
32
+ const { allowsFunctional } = useCookieConsent();
11
33
 
12
- // On client mount, read persisted theme and apply if different
13
- if (import.meta.client) {
14
- onMounted(() => {
15
- const stored = localStorage.getItem(STORAGE_KEY);
16
- if (stored && isValidThemeId(stored) && stored !== themeId.value) {
17
- themeId.value = stored;
18
- applyThemeToElement(document.documentElement, stored);
19
- }
20
- });
21
- }
34
+ function setDarkMode(dark: boolean): void {
35
+ isDark.value = dark;
36
+
37
+ // Only persist to cookie if user consented to functional cookies
38
+ if (allowsFunctional.value) {
39
+ schemeCookie.value = dark ? 'dark' : 'light';
40
+ }
41
+
42
+ // Resolve the correct variant for this family + mode
43
+ const family = THEME_TO_FAMILY[instanceDefault.value] ?? 'classic';
44
+ const variants = FAMILY_VARIANTS[family] ?? FAMILY_VARIANTS.classic!;
45
+ const newTheme = dark ? variants.dark : variants.light;
46
+
47
+ themeId.value = newTheme;
22
48
 
23
- function setTheme(id: string): void {
24
- if (isValidThemeId(id)) {
25
- themeId.value = id;
26
- if (import.meta.client) {
27
- applyThemeToElement(document.documentElement, id);
28
- localStorage.setItem(STORAGE_KEY, id);
29
- }
49
+ if (import.meta.client) {
50
+ applyThemeToElement(document.documentElement, newTheme);
30
51
  }
31
52
  }
32
53
 
33
- return { themeId, setTheme };
54
+ return { themeId, isDark, instanceDefault, setDarkMode };
34
55
  }
package/layouts/admin.vue CHANGED
@@ -31,6 +31,7 @@ const sidebarOpen = ref(false);
31
31
  <NuxtLink to="/admin/content" class="admin-nav-link" @click="sidebarOpen = false"><i class="fa-solid fa-newspaper"></i> Content</NuxtLink>
32
32
  <NuxtLink to="/admin/reports" class="admin-nav-link" @click="sidebarOpen = false"><i class="fa-solid fa-flag"></i> Reports</NuxtLink>
33
33
  <NuxtLink to="/admin/audit" class="admin-nav-link" @click="sidebarOpen = false"><i class="fa-solid fa-clipboard-list"></i> Audit Log</NuxtLink>
34
+ <NuxtLink to="/admin/theme" class="admin-nav-link" @click="sidebarOpen = false"><i class="fa-solid fa-palette"></i> Theme</NuxtLink>
34
35
  <NuxtLink to="/admin/federation" class="admin-nav-link" @click="sidebarOpen = false"><i class="fa-solid fa-globe"></i> Federation</NuxtLink>
35
36
  <NuxtLink to="/admin/settings" class="admin-nav-link" @click="sidebarOpen = false"><i class="fa-solid fa-gear"></i> Settings</NuxtLink>
36
37
  </nav>
@@ -183,14 +183,19 @@ const userUsername = computed(() => user.value?.username ?? '');
183
183
  <h4 class="cpub-footer-col-title">Platform</h4>
184
184
  <NuxtLink to="/about" class="cpub-footer-link">About</NuxtLink>
185
185
  <NuxtLink v-if="docs" to="/docs" class="cpub-footer-link">Docs</NuxtLink>
186
+ <NuxtLink to="/privacy" class="cpub-footer-link">Privacy Policy</NuxtLink>
187
+ <NuxtLink to="/cookies" class="cpub-footer-link">Cookie Policy</NuxtLink>
188
+ <NuxtLink to="/terms" class="cpub-footer-link">Terms of Service</NuxtLink>
186
189
  <a href="/feed.xml" class="cpub-footer-link">RSS Feed</a>
187
- <a href="/sitemap.xml" class="cpub-footer-link">Sitemap</a>
188
190
  </nav>
189
191
  </div>
190
192
  <div class="cpub-footer-bottom">
191
193
  <span>&copy; {{ new Date().getFullYear() }} {{ siteName }}. Open source under MIT.</span>
192
194
  </div>
193
195
  </footer>
196
+
197
+ <!-- Cookie consent banner -->
198
+ <CookieConsent />
194
199
  </div>
195
200
  </template>
196
201
 
package/nuxt.config.ts CHANGED
@@ -20,12 +20,28 @@ export default defineNuxtConfig({
20
20
  href: 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css',
21
21
  crossorigin: 'anonymous',
22
22
  },
23
+ {
24
+ rel: 'preconnect',
25
+ href: 'https://fonts.googleapis.com',
26
+ },
27
+ {
28
+ rel: 'preconnect',
29
+ href: 'https://fonts.gstatic.com',
30
+ crossorigin: 'anonymous',
31
+ },
32
+ {
33
+ rel: 'stylesheet',
34
+ href: 'https://fonts.googleapis.com/css2?family=Fraunces:ital,opsz,wght@0,9..144,300..900;1,9..144,300..900&family=Work+Sans:ital,wght@0,300..800;1,300..800&display=swap',
35
+ },
23
36
  ],
24
37
  },
25
38
  },
26
39
  css: [
27
40
  uiTheme('base.css'),
28
41
  uiTheme('dark.css'),
42
+ uiTheme('generics.css'),
43
+ uiTheme('agora.css'),
44
+ uiTheme('agora-dark.css'),
29
45
  uiTheme('components.css'),
30
46
  uiTheme('prose.css'),
31
47
  uiTheme('layouts.css'),
@@ -71,6 +87,7 @@ export default defineNuxtConfig({
71
87
  },
72
88
  contentTypes: 'project,article,blog,explainer',
73
89
  contestCreation: 'admin',
90
+ instanceCookies: [] as Array<{ name: string; category: string; description: string; duration: string; provider?: string }>,
74
91
  },
75
92
  },
76
93
  routeRules: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@commonpub/layer",
3
- "version": "0.3.37",
3
+ "version": "0.4.0",
4
4
  "type": "module",
5
5
  "main": "./nuxt.config.ts",
6
6
  "files": [
@@ -16,6 +16,7 @@
16
16
  "server",
17
17
  "theme",
18
18
  "types",
19
+ "utils",
19
20
  "!**/__tests__/",
20
21
  "!**/*.test.ts",
21
22
  "!**/*.spec.ts",
@@ -49,15 +50,15 @@
49
50
  "vue": "^3.4.0",
50
51
  "vue-router": "^4.3.0",
51
52
  "zod": "^4.3.6",
53
+ "@commonpub/config": "0.8.0",
52
54
  "@commonpub/auth": "0.5.0",
53
- "@commonpub/config": "0.7.1",
54
55
  "@commonpub/editor": "0.5.0",
55
- "@commonpub/learning": "0.5.0",
56
56
  "@commonpub/docs": "0.5.2",
57
- "@commonpub/protocol": "0.9.5",
57
+ "@commonpub/learning": "0.5.0",
58
+ "@commonpub/ui": "0.8.0",
59
+ "@commonpub/server": "2.22.0",
58
60
  "@commonpub/schema": "0.8.13",
59
- "@commonpub/server": "2.20.1",
60
- "@commonpub/ui": "0.7.1"
61
+ "@commonpub/protocol": "0.9.5"
61
62
  },
62
63
  "devDependencies": {
63
64
  "@testing-library/jest-dom": "^6.9.1",