Skip to content

πŸ”₯ πŸ”₯ πŸ”₯ A Free & Self-hostable Airtable Alternative

feat: dark mode script and toggle

DarkPhoenix270415e96c1ee324
diff --git a/packages/nc-gui/app.vue b/packages/nc-gui/app.vue
index 2462da1003..f3fb0ac458 100644
--- a/packages/nc-gui/app.vue
+++ b/packages/nc-gui/app.vue
@@ -7,12 +7,16 @@ const route = router.currentRoute
const disableBaseLayout = computed(() => route.value.path.startsWith('/nc/view') || route.value.path.startsWith('/nc/form'))
-useTheme()
-
-const { isExperimentalFeatureModalOpen, initializeFeatures } = useBetaFeatureToggle()
+const { isExperimentalFeatureModalOpen, initializeFeatures, isFeatureEnabled } = useBetaFeatureToggle()
initializeFeatures()
+const isDarkModeEnabled = computed(() => isFeatureEnabled(FEATURE_FLAG.DARK_MODE))
+
+if (isDarkModeEnabled.value) {
+ useTheme()
+}
+
const { commandPalette, cmdData, cmdPlaceholder, activeScope, loadTemporaryScope } = useCommandPalette()
const { cmdK, cmdL, cmdJ, setActiveCmdView } = useCommand()
diff --git a/packages/nc-gui/assets/nc-icons-v2/sun.svg b/packages/nc-gui/assets/nc-icons-v2/sun.svg
new file mode 100644
index 0000000000..aec2d47b18
--- /dev/null
+++ b/packages/nc-gui/assets/nc-icons-v2/sun.svg
@@ -0,0 +1,18 @@
+<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
+<g clip-path="url(#clip0_184_5146)">
+<path d="M8 14V15.3333" stroke="currentColor" stroke-width="1.33333" stroke-linecap="round" stroke-linejoin="round"/>
+<path d="M12.2401 12.24L13.1867 13.1867" stroke="currentColor" stroke-width="1.33333" stroke-linecap="round" stroke-linejoin="round"/>
+<path d="M2.81329 13.1867L3.75996 12.24" stroke="currentColor" stroke-width="1.33333" stroke-linecap="round" stroke-linejoin="round"/>
+<path d="M14 8H15.3333" stroke="currentColor" stroke-width="1.33333" stroke-linecap="round" stroke-linejoin="round"/>
+<path d="M0.666687 8H2.00002" stroke="currentColor" stroke-width="1.33333" stroke-linecap="round" stroke-linejoin="round"/>
+<path d="M8.00002 11.3334C9.84097 11.3334 11.3334 9.84097 11.3334 8.00002C11.3334 6.15907 9.84097 4.66669 8.00002 4.66669C6.15907 4.66669 4.66669 6.15907 4.66669 8.00002C4.66669 9.84097 6.15907 11.3334 8.00002 11.3334Z" stroke="currentColor" stroke-width="1.33333" stroke-linecap="round" stroke-linejoin="round"/>
+<path d="M12.2401 3.75996L13.1867 2.81329" stroke="currentColor" stroke-width="1.33333" stroke-linecap="round" stroke-linejoin="round"/>
+<path d="M2.81329 2.81329L3.75996 3.75996" stroke="currentColor" stroke-width="1.33333" stroke-linecap="round" stroke-linejoin="round"/>
+<path d="M8 0.666687V2.00002" stroke="currentColor" stroke-width="1.33333" stroke-linecap="round" stroke-linejoin="round"/>
+</g>
+<defs>
+<clipPath id="clip0_184_5146">
+<rect width="16" height="16" fill="white"/>
+</clipPath>
+</defs>
+</svg>
diff --git a/packages/nc-gui/components/ai/Erd/utils.ts b/packages/nc-gui/components/ai/Erd/utils.ts
index 31b5f5588c..8fda4f1666 100644
--- a/packages/nc-gui/components/ai/Erd/utils.ts
+++ b/packages/nc-gui/components/ai/Erd/utils.ts
@@ -70,9 +70,9 @@ interface Relation {
export function useErdElements(schema: MaybeRef<AiBaseSchema>, props: MaybeRef<AiERDConfig>) {
const elements = ref<Elements<AiNodeData | AiEdgeData>>([])
- const { theme } = useTheme()
-
- const colorScale = d3ScaleLinear<string>().domain([0, 2]).range([theme.value.primaryColor, theme.value.accentColor])
+ const colorScale = d3ScaleLinear<string>()
+ .domain([0, 2])
+ .range([themeV2Colors['royal-blue'].DEFAULT, themeV2Colors.pink['500']])
const dagreGraph = new dagre.graphlib.Graph()
dagreGraph.setDefaultEdgeLabel(() => ({}))
diff --git a/packages/nc-gui/components/dashboard/MiniSidebar.vue b/packages/nc-gui/components/dashboard/MiniSidebar.vue
index 43a41a2649..aad456adeb 100644
--- a/packages/nc-gui/components/dashboard/MiniSidebar.vue
+++ b/packages/nc-gui/components/dashboard/MiniSidebar.vue
@@ -245,6 +245,10 @@ useEventListener(document, 'keydown', async (e: KeyboardEvent) => {
</template>
</div>
<div class="flex flex-col items-center">
+ <DashboardMiniSidebarItemWrapper>
+ <DashboardMiniSidebarTheme />
+ </DashboardMiniSidebarItemWrapper>
+
<DashboardMiniSidebarItemWrapper>
<NcTooltip :title="$t('general.help')" placement="right" hide-on-click :arrow="false">
<DashboardMiniSidebarHelp />
diff --git a/packages/nc-gui/components/dashboard/MiniSidebar/Theme.vue b/packages/nc-gui/components/dashboard/MiniSidebar/Theme.vue
new file mode 100644
index 0000000000..3ac837b4d7
--- /dev/null
+++ b/packages/nc-gui/components/dashboard/MiniSidebar/Theme.vue
@@ -0,0 +1,11 @@
+<script setup lang="ts">
+const { toggleTheme, isDark } = useTheme()
+</script>
+
+<template>
+ <div v-e="['c:nocodb:theme']" class="nc-mini-sidebar-btn-full-width" data-testid="nc-sidebar-theme">
+ <div class="nc-mini-sidebar-btn relative" @click="toggleTheme">
+ <GeneralIcon :icon="isDark ? 'ncMoon' : 'ncSun'" class="h-4 w-4" />
+ </div>
+ </div>
+</template>
diff --git a/packages/nc-gui/components/erd/utils.ts b/packages/nc-gui/components/erd/utils.ts
index a2ba3ceb8b..9e6412ac80 100644
--- a/packages/nc-gui/components/erd/utils.ts
+++ b/packages/nc-gui/components/erd/utils.ts
@@ -54,9 +54,9 @@ interface Relation {
export function useErdElements(tables: MaybeRef<TableType[]>, props: MaybeRef<ERDConfig>) {
const elements = ref<Elements<NodeData | EdgeData>>([])
- const { theme } = useTheme()
-
- const colorScale = d3ScaleLinear<string>().domain([0, 2]).range([theme.value.primaryColor, theme.value.accentColor])
+ const colorScale = d3ScaleLinear<string>()
+ .domain([0, 2])
+ .range([themeV2Colors['royal-blue'].DEFAULT, themeV2Colors.pink['500']])
const dagreGraph = new dagre.graphlib.Graph()
dagreGraph.setDefaultEdgeLabel(() => ({}))
diff --git a/packages/nc-gui/composables/useBetaFeatureToggle.ts b/packages/nc-gui/composables/useBetaFeatureToggle.ts
index bc09d85082..ef4775252a 100644
--- a/packages/nc-gui/composables/useBetaFeatureToggle.ts
+++ b/packages/nc-gui/composables/useBetaFeatureToggle.ts
@@ -16,6 +16,24 @@ const FEATURES = [
enabled: !ncIsPlaywright(),
version: 1,
},
+ {
+ id: 'dashboard',
+ title: 'Dashboard',
+ isEngineering: true,
+ description: 'Build interactive dashboards with charts, widgets, and visualizations to monitor your data at a glance.',
+ enabled: false,
+ version: 1,
+ isEE: true,
+ },
+ {
+ id: 'dark_mode',
+ title: 'Dark Mode',
+ isEngineering: true,
+ description: 'Keep your eyes healthy with dark mode.',
+ enabled: false,
+ version: 1,
+ isEE: true,
+ },
{
id: 'canvas_group_grid_view',
title: 'Improved Group By',
diff --git a/packages/nc-gui/composables/useTheme.ts b/packages/nc-gui/composables/useTheme.ts
new file mode 100644
index 0000000000..715fef21d0
--- /dev/null
+++ b/packages/nc-gui/composables/useTheme.ts
@@ -0,0 +1,67 @@
+import { computed, onMounted, ref, watch } from 'vue'
+
+export type ThemeMode = 'system' | 'light' | 'dark'
+
+export const useTheme = createSharedComposable(() => {
+ const selectedTheme = ref<ThemeMode>('system')
+ const systemPreference = ref<'light' | 'dark'>('light')
+
+ const isDark = computed(() => {
+ if (selectedTheme.value === 'system') {
+ return systemPreference.value === 'dark'
+ }
+ return selectedTheme.value === 'dark'
+ })
+
+ const applyTheme = (dark: boolean) => {
+ if (typeof document !== 'undefined') {
+ if (dark) {
+ document.documentElement.setAttribute('theme', 'dark')
+ } else {
+ document.documentElement.removeAttribute('theme')
+ }
+ }
+ }
+
+ const setTheme = (theme: ThemeMode) => {
+ selectedTheme.value = theme
+ if (typeof localStorage !== 'undefined') {
+ localStorage.setItem('nc-theme', theme)
+ }
+ }
+
+ const toggleTheme = () => {
+ setTheme(selectedTheme.value === 'dark' ? 'light' : 'dark')
+ }
+
+ let initialized = false
+
+ const init = () => {
+ if (initialized || typeof window === 'undefined') return
+ initialized = true
+
+ const saved = localStorage.getItem('nc-theme') as ThemeMode
+ if (saved && ['system', 'light', 'dark'].includes(saved)) {
+ selectedTheme.value = saved
+ }
+
+ const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)')
+ systemPreference.value = mediaQuery.matches ? 'dark' : 'light'
+
+ mediaQuery.addEventListener('change', (e) => {
+ systemPreference.value = e.matches ? 'dark' : 'light'
+ })
+ }
+
+ watch(isDark, applyTheme, { immediate: true })
+
+ onMounted(init)
+
+ return {
+ selectedTheme,
+ isDark,
+ setTheme,
+ toggleTheme,
+ init,
+ }
+})
diff --git a/packages/nc-gui/composables/useTheme/index.ts b/packages/nc-gui/composables/useTheme/index.ts
deleted file mode 100644
index 2dce7d08a8..0000000000
--- a/packages/nc-gui/composables/useTheme/index.ts
+++ /dev/null
@@ -1,44 +0,0 @@
-import { ConfigProvider } from 'ant-design-vue'
-import tinycolor from 'tinycolor2'
-
-export const useTheme = createGlobalState((config?: Partial<ThemeConfig>) => {
- const primaryColor = useCssVar('--color-primary', typeof document !== 'undefined' ? document.documentElement : null)
- const accentColor = useCssVar('--color-accent', typeof document !== 'undefined' ? document.documentElement : null)
- const defaultTheme = {
- primaryColor: themeV2Colors['royal-blue'].DEFAULT,
- accentColor: themeV2Colors.pink['500'],
- }
-
- /** current theme config */
- const currentTheme = ref(defaultTheme)
-
- /** set initial config */
- setTheme(config ?? currentTheme.value)
-
- /** set theme */
- function setTheme(theme?: Partial<ThemeConfig>) {
- const themePrimary = theme?.primaryColor ? tinycolor(theme.primaryColor) : tinycolor(themeV2Colors['royal-blue'].DEFAULT)
- const themeAccent = theme?.accentColor ? tinycolor(theme.accentColor) : tinycolor(themeV2Colors.pink['500'])
-
- // convert hex colors to rgb values
- primaryColor.value = themePrimary.isValid()
- ? hexToRGB(themePrimary.toHex8String())
- : hexToRGB(themeV2Colors['royal-blue'].DEFAULT)
- accentColor.value = themeAccent.isValid() ? hexToRGB(themeAccent.toHex8String()) : hexToRGB(themeV2Colors.pink['500'])
-
- currentTheme.value = {
- primaryColor: themePrimary.toHex8String().toUpperCase().slice(0, -2),
- accentColor: themeAccent.toHex8String().toUpperCase().slice(0, -2),
- }
-
- ConfigProvider.config({
- theme: currentTheme.value,
- })
- }
-
- return {
- theme: currentTheme,
- setTheme,
- defaultTheme,
- }
-})
diff --git a/packages/nc-gui/pages/index/[typeOrId]/[baseId]/index.vue b/packages/nc-gui/pages/index/[typeOrId]/[baseId]/index.vue
index fc2fac69e5..9932a0e414 100644
--- a/packages/nc-gui/pages/index/[typeOrId]/[baseId]/index.vue
+++ b/packages/nc-gui/pages/index/[typeOrId]/[baseId]/index.vue
@@ -4,8 +4,6 @@ definePageMeta({
hasSidebar: true,
})
-useTheme()
-
const { t } = useI18n()
const { $e } = useNuxtApp()
diff --git a/packages/nc-gui/store/base.ts b/packages/nc-gui/store/base.ts
index 30e9630de5..289d651d02 100644
--- a/packages/nc-gui/store/base.ts
+++ b/packages/nc-gui/store/base.ts
@@ -12,8 +12,6 @@ export const useBase = defineStore('baseStore', () => {
const route = router.currentRoute
- const { setTheme, theme } = useTheme()
-
const { loadRoles } = useRoles()
const { refreshCommandPalette } = useCommandPalette()
@@ -197,7 +195,7 @@ export const useBase = defineStore('baseStore', () => {
}
async function saveTheme(_theme: Partial<ThemeConfig>) {
- const fullTheme = {
+ /* const fullTheme = {
primaryColor: theme.value.primaryColor,
accentColor: theme.value.accentColor,
..._theme,
@@ -210,10 +208,9 @@ export const useBase = defineStore('baseStore', () => {
theme: fullTheme,
},
})
-
+*/
// setTheme(fullTheme)
-
- $e('c:themes:change')
+ // $e('c:themes:change')
}
async function hasEmptyOrNullFilters() {
@@ -224,7 +221,7 @@ export const useBase = defineStore('baseStore', () => {
// base.value = {}
// tables.value = []
baseMetaInfo.value = undefined
- setTheme()
+ // setTheme()
}
const setProject = (baseVal: BaseType) => {
diff --git a/packages/nc-gui/store/workspace.ts b/packages/nc-gui/store/workspace.ts
index e980eb410d..7ad77ece97 100644
--- a/packages/nc-gui/store/workspace.ts
+++ b/packages/nc-gui/store/workspace.ts
@@ -23,10 +23,6 @@ export const useWorkspace = defineStore('workspaceStore', () => {
const lastPopulatedWorkspaceId = ref<string | null>(null)
- const { setTheme, theme } = useTheme()
-
- const { $e } = useNuxtApp()
-
const { appInfo, ncNavigateTo } = useGlobal()
const workspaces = ref<Map<string, any>>(new Map())
@@ -179,7 +175,8 @@ export const useWorkspace = defineStore('workspaceStore', () => {
const moveWorkspace = async (..._args: any) => {}
async function saveTheme(_theme: Partial<ThemeConfig>) {
- const fullTheme = {
+ // Not Implemented
+ /* const fullTheme = {
primaryColor: theme.value.primaryColor,
accentColor: theme.value.accentColor,
..._theme,
@@ -194,7 +191,7 @@ export const useWorkspace = defineStore('workspaceStore', () => {
setTheme(fullTheme)
- $e('c:themes:change')
+ $e('c:themes:change') */
}
async function clearWorkspaces() {
diff --git a/packages/nc-gui/utils/iconUtils.ts b/packages/nc-gui/utils/iconUtils.ts
index b10d61520d..54c396d965 100644
--- a/packages/nc-gui/utils/iconUtils.ts
+++ b/packages/nc-gui/utils/iconUtils.ts
@@ -749,6 +749,7 @@ import NcPlusCircleSolid from '~icons/nc-icons-v2/plus-circle-solid.svg'
import NcHeadphone from '~icons/nc-icons-v2/headphone'
import NcPaintRoller from '~icons/nc-icons-v2/paint-roller.svg'
import NcConditions from '~icons/nc-icons-v2/conditions.svg'
+import NcSun from '~icons/nc-icons-v2/sun.svg'
import MdiNcCheckBold from '~icons/nc-icons/mdi-check-bold'
import MdiNcCropSquare from '~icons/nc-icons/mdi-crop-square'
@@ -1747,6 +1748,7 @@ export const iconMap = {
'ncChartLine': h(NcChartLine, { stroke: 'transparent' }),
'ncChartDonut': h(NcChartDonut, { stroke: 'transparent' }),
'ncChartPie': h(NcChartPie, { stroke: 'transparent' }),
+ 'ncSun': NcSun,
}
export const getMdiIcon = (type: string): any => {
@@ -4356,6 +4358,10 @@ export const searchableMap = {
icon: NcMultiCircle,
keywords: ['multi', 'circle', 'multi-circle', 'multi-circle-outline'],
},
+ ncSun: {
+ icon: NcSun,
+ keywords: ['sun', 'sunny', 'weather', 'light', 'day', 'morning', 'summer', 'warm', 'warmth'],
+ },
}
export const searchIcons = (searchTerm: string) => {