Skip to content

Commit

Permalink
expose the used config via tw.config
Browse files Browse the repository at this point in the history
  • Loading branch information
sastan committed Feb 1, 2022
1 parent b223e5b commit 9203734
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/sweet-kings-ring.md
@@ -0,0 +1,5 @@
---
'twind': patch
---

expose the used config via `tw.config`
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -56,13 +56,13 @@
"name": "@twind/cdn",
"path": "packages/cdn/dist/cdn.esnext.js",
"brotli": true,
"limit": "15kb"
"limit": "15.1kb"
},
{
"name": "@twind/tailwind",
"path": "packages/tailwind/dist/tailwind.esnext.js",
"brotli": true,
"limit": "14.95kb"
"limit": "15kb"
}
],
"// start — use 'BROWSER=none' to prevent vite to open a browser if within codesandbox or stackblitz": "",
Expand Down
2 changes: 1 addition & 1 deletion packages/cdn/package.json
Expand Up @@ -32,7 +32,7 @@
"name": "@twind/cdn",
"path": "dist/cdn.esnext.js",
"brotli": true,
"limit": "15kb"
"limit": "15.1kb"
}
],
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/cdn/src/index.ts
Expand Up @@ -52,5 +52,5 @@ export function setup(
): Twind {
cancelAutoSetup()

return observe(twind(defineConfig(config as TwindConfig), sheet), target)
return observe(twind(defineConfig(config as TwindConfig), sheet), target) as unknown as Twind
}
2 changes: 1 addition & 1 deletion packages/tailwind/src/index.ts
Expand Up @@ -70,7 +70,7 @@ export function setup(
sheet?: Sheet,
target?: HTMLElement,
): Twind {
return setup$(defineConfig(config as TwindUserConfig), sheet, target)
return setup$(defineConfig(config as TwindUserConfig), sheet, target) as unknown as Twind
}

// If we run in the browser as `<script src="..."></script>` patch twind to use our setup and defineConfig
Expand Down
2 changes: 1 addition & 1 deletion packages/twind/src/observe.ts
Expand Up @@ -4,7 +4,7 @@ import { changed } from './internal/changed'
import { tw as tw$ } from './runtime'

export function observe<Theme extends BaseTheme = BaseTheme, Target = unknown>(
tw: Twind<Theme, Target> = tw$ as Twind<Theme, Target>,
tw: Twind<Theme, Target> = tw$ as unknown as Twind<Theme, Target>,
target = typeof document != 'undefined' && document.documentElement,
): Twind<Theme, Target> {
if (!target) return tw
Expand Down
8 changes: 5 additions & 3 deletions packages/twind/src/runtime.ts
Expand Up @@ -60,7 +60,9 @@ export const tw = /* @__PURE__ */ Object.defineProperties(
theme(...args: unknown[]) {
return active.theme(...(args as []))
},

get config() {
return active.config
},
clear() {
return active.clear()
},
Expand Down Expand Up @@ -108,7 +110,7 @@ export function setup<Theme extends BaseTheme = BaseTheme, SheetTarget = unknown
active.destroy()
}

active = observe(twind(config as TwindUserConfig<Theme>, sheet), target)
active = observe(twind(config as TwindUserConfig, sheet), target)

if (firstRun && typeof document != 'undefined') {
// first run in browser
Expand All @@ -120,5 +122,5 @@ export function setup<Theme extends BaseTheme = BaseTheme, SheetTarget = unknown
}
}

return active as Twind<Theme, SheetTarget>
return active as unknown as Twind<Theme, SheetTarget>
}
26 changes: 26 additions & 0 deletions packages/twind/src/tests/twind.test.ts
@@ -0,0 +1,26 @@
import { assert, test, afterEach } from 'vitest'

import { twind, virtual, css } from '..'

const tw = twind(
{
rules: [['bg-', 'background']],
},
virtual(),
)

afterEach(() => tw.clear())

test('the config can accessed', () => {
assert.isFunction(tw.config.stringify)
assert.deepEqual(tw.config, {
preflight: [],
darkMode: undefined,
theme: { extend: {} },
variants: [['dark', '@media (prefers-color-scheme:dark)']],
rules: [['bg-', 'background']],
ignorelist: [],
hash: undefined,
stringify: tw.config.stringify,
})
})
2 changes: 2 additions & 0 deletions packages/twind/src/twind.ts
Expand Up @@ -125,6 +125,8 @@ export function twind(userConfig: TwindConfig<any> | TwindUserConfig<any>, sheet

theme: context.theme,

config,

clear() {
sheet.clear()
insertedRules.clear()
Expand Down
4 changes: 3 additions & 1 deletion packages/twind/src/types.ts
Expand Up @@ -73,7 +73,9 @@ export interface Twind<Theme extends BaseTheme = BaseTheme, Target = unknown> {

readonly target: Target

theme: ThemeFunction<ExtractUserTheme<Theme>>
readonly theme: ThemeFunction<ExtractUserTheme<Theme>>

readonly config: TwindConfig<Theme>

/** Clears all CSS rules from the sheet. */
clear(): void
Expand Down

0 comments on commit 9203734

Please sign in to comment.