Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit 2e38bab

Browse files
committed
chore: initialize packages
1 parent adc953d commit 2e38bab

File tree

17 files changed

+932
-0
lines changed

17 files changed

+932
-0
lines changed

packages/c-color-mode/package.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "@chakra-ui/c-color-mode",
3+
"version": "1.0.0",
4+
"main": "dist/cjs/index.js",
5+
"module": "dist/esm/index.js",
6+
"types": "dist/types/index.d.ts",
7+
"typings": "dist/types/index.d.ts",
8+
"author": "Jonathan Bakebwa <codebender828@gmail.com>",
9+
"homepage": "https://github.com/chakra-ui/chakra-ui-vue-next#readme",
10+
"license": "MIT",
11+
"files": [
12+
"dist"
13+
],
14+
15+
"publishConfig": {
16+
"access": "public"
17+
},
18+
"repository": {
19+
"type": "git",
20+
"url": "git+https://github.com/chakra-ui/chakra-ui-vue-next.git"
21+
},
22+
"bugs": {
23+
"url": "https://github.com/chakra-ui/chakra-ui=vue-next/issues"
24+
},
25+
"sideEffects": false,
26+
"scripts": {
27+
"build": "concurrently yarn:build:*",
28+
"build:esm": "cross-env BABEL_ENV=esm babel src --root-mode upward --extensions .ts,.tsx -d dist/esm --source-maps",
29+
"build:cjs": "cross-env BABEL_ENV=cjs babel src --root-mode upward --extensions .ts,.tsx -d dist/cjs --source-maps",
30+
"build:types": "cross-env tsc --emitDeclarationOnly --declaration --declarationDir dist/types",
31+
"watch": "concurrently yarn:watch:*",
32+
"watch:esm": "cross-env BABEL_ENV=esm babel src --root-mode upward --extensions .ts,.tsx -d dist/esm --source-maps --watch",
33+
"watch:cjs": "cross-env BABEL_ENV=cjs babel src --root-mode upward --extensions .ts,.tsx -d dist/cjs --source-maps --watch",
34+
"watch:types": "cross-env tsc --emitDeclarationOnly --declaration --declarationDir dist/types --watch"
35+
},
36+
"dependencies": {
37+
"@chakra-ui/vue-utils": "*"
38+
},
39+
"peerDependencies": {
40+
"vue": ">=3.0.5"
41+
}
42+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { __DEV__ } from '@chakra-ui/vue-utils'
2+
import { inject, ref } from 'vue'
3+
import { ColorMode } from './color-mode.utils'
4+
5+
export type { ColorMode }
6+
7+
export interface ColorModeOptions {
8+
initialColorMode?: ColorMode
9+
useSystemColorMode?: boolean
10+
}
11+
12+
interface ColorModeContextType {
13+
colorMode: ColorMode
14+
toggleColorMode: () => void
15+
setColorMode: (value: any) => void
16+
}
17+
18+
/** Injects color mode into component instance */
19+
export const useColorMode = () => {
20+
const _colorMode = inject('$chakraColorMode') as ColorMode
21+
const colorMode = ref(_colorMode)
22+
23+
const toggleColorMode = () => {
24+
colorMode.value = 'light'
25+
? (colorMode.value = 'dark')
26+
: (colorMode.value = 'light')
27+
}
28+
29+
return {
30+
colorMode,
31+
toggleColorMode,
32+
}
33+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { isBrowser, noop } from '@chakra-ui/vue-utils'
2+
3+
const classNames = {
4+
light: 'chakra-ui-light',
5+
dark: 'chakra-ui-dark',
6+
}
7+
8+
export type ColorMode = 'light' | 'dark'
9+
10+
/**
11+
* SSR: Graceful fallback for the `body` element
12+
*/
13+
const mockBody = {
14+
classList: { add: noop, remove: noop },
15+
}
16+
17+
const getBody = () => (isBrowser ? document.body : mockBody)
18+
19+
/**
20+
* Function to add/remove class from `body` based on color mode
21+
*/
22+
export function syncBodyClassName(isDark: boolean) {
23+
const body = getBody()
24+
body.classList.add(isDark ? classNames.dark : classNames.light)
25+
body.classList.remove(isDark ? classNames.light : classNames.dark)
26+
}
27+
28+
/**
29+
* Check if JS media query matches the query string passed
30+
*/
31+
function getMediaQuery(query: string) {
32+
const mediaQueryList = window.matchMedia?.(query)
33+
if (!mediaQueryList) {
34+
return undefined
35+
}
36+
return !!mediaQueryList.media === mediaQueryList.matches
37+
}
38+
39+
export const queries = {
40+
light: '(prefers-color-scheme: light)',
41+
dark: '(prefers-color-scheme: dark)',
42+
}
43+
44+
export const lightQuery = queries.light
45+
export const darkQuery = queries.dark
46+
47+
export function getColorScheme(fallback?: ColorMode) {
48+
const isDark = getMediaQuery(queries.dark) ?? fallback === 'dark'
49+
return isDark ? 'dark' : 'light'
50+
}
51+
52+
/**
53+
* Adds system os color mode listener, and run the callback
54+
* once preference changes
55+
*/
56+
export function addListener(fn: Function) {
57+
if (!('matchMedia' in window)) {
58+
return noop
59+
}
60+
61+
const mediaQueryList = window.matchMedia(queries.dark)
62+
63+
const listener = () => {
64+
fn(mediaQueryList.matches ? 'dark' : 'light')
65+
}
66+
67+
listener()
68+
mediaQueryList.addListener(listener)
69+
70+
return () => {
71+
mediaQueryList.removeListener(listener)
72+
}
73+
}
74+
75+
export const root = {
76+
get: () =>
77+
document.documentElement.style.getPropertyValue(
78+
'--chakra-ui-color-mode'
79+
) as ColorMode,
80+
set: (mode: ColorMode) => {
81+
if (isBrowser) {
82+
document.documentElement.style.setProperty('--chakra-ui-color-mode', mode)
83+
}
84+
},
85+
}

packages/c-color-mode/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './color-mode-provider'
2+
export * from './storage-manager'
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { __DEV__ } from '@chakra-ui/vue-utils'
2+
import { ColorMode } from './color-mode.utils'
3+
4+
const hasSupport = () => typeof Storage !== 'undefined'
5+
export const storageKey = 'chakra-ui-color-mode'
6+
7+
type MaybeColorMode = ColorMode | undefined
8+
9+
export interface StorageManager {
10+
get(init?: ColorMode): MaybeColorMode
11+
set(value: ColorMode): void
12+
type: 'cookie' | 'localStorage'
13+
}
14+
15+
/**
16+
* Simple object to handle read-write to localStorage
17+
*/
18+
export const localStorageManager: StorageManager = {
19+
get(init?) {
20+
if (!hasSupport()) return init
21+
try {
22+
const value = localStorage.getItem(storageKey) as MaybeColorMode
23+
return value ?? init
24+
} catch (error) {
25+
if (__DEV__) {
26+
console.log(error)
27+
}
28+
return init
29+
}
30+
},
31+
set(value) {
32+
if (!hasSupport()) return
33+
try {
34+
localStorage.setItem(storageKey, value)
35+
} catch (error) {
36+
if (__DEV__) {
37+
console.log(error)
38+
}
39+
}
40+
},
41+
type: 'localStorage',
42+
}
43+
44+
/**
45+
* Simple object to handle read-write to cookies
46+
*/
47+
export const cookieStorageManager = (cookies = ''): StorageManager => ({
48+
get(init?) {
49+
const match = cookies.match(new RegExp(`(^| )${storageKey}=([^;]+)`))
50+
51+
if (match) {
52+
return match[2] as ColorMode
53+
}
54+
55+
return init
56+
},
57+
set(value) {
58+
document.cookie = `${storageKey}=${value}; max-age=31536000; path=/`
59+
},
60+
type: 'cookie',
61+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { CColorMode } from '../'
2+
3+
4+
it('should be truthy', () => {
5+
expect(1).toBe(1)
6+
})

packages/c-color-mode/tsconfig.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"include": ["src"]
4+
}

0 commit comments

Comments
 (0)