Skip to content

Commit

Permalink
feat: createGlobal API
Browse files Browse the repository at this point in the history
fixes #10
  • Loading branch information
estrattonbailey committed Jan 5, 2022
1 parent 3f7d632 commit 6dc9855
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,28 @@ test('prefix', () => {
assert.equal(/hypo/.test(sheet), true)
})

test('createGlobal', () => {
const { createGlobal } = hypostyle(defaults)

const sheet = createGlobal({
'@media (min-width: 567px)': {
color: 'tomato',
},
})

assert.ok(/color:tomato/.test(sheet))
})

test('no theme, defaults to presets', () => {
const { createGlobal } = hypostyle()

const sheet = createGlobal({
c: 'tomato',
})

assert.ok(/color:tomato/.test(sheet))
})

/**
* @see https://github.com/sure-thing/hypostyle/issues/7
*/
Expand Down
10 changes: 10 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ function explode(props: HypostyleObject, theme: Theme) {

// expand macros and variants, copy other props
for (var prop in props) {
/* c8 ignore next */
if (!props.hasOwnProperty(prop)) continue // skip proto

// macro exists AND prop is true
Expand All @@ -107,6 +108,7 @@ function explode(props: HypostyleObject, theme: Theme) {

// recursively expand all shorthands
for (var prop in styles) {
/* c8 ignore next */
if (!styles.hasOwnProperty(prop)) continue // skip proto

var value = styles[prop]
Expand Down Expand Up @@ -141,6 +143,7 @@ function style(props: HypostyleObject, theme: Theme): CssLikeObject {
var styles = {}

for (var prop in props) {
/* c8 ignore next */
if (!props.hasOwnProperty(prop)) continue // skip proto

var mixedObject: HypostyleObject | HypostyleValue = props[prop]
Expand Down Expand Up @@ -210,6 +213,7 @@ function pick<T>(props: HypostyleObject, theme: Theme): { styles: HypostyleObjec
var extra = {}

for (var prop in props) {
/* c8 ignore next */
if (!props.hasOwnProperty(prop)) continue // skip proto

if (theme.macros[prop] || theme.variants[prop] || theme.shorthands[prop] || theme.properties[prop]) {
Expand All @@ -228,6 +232,7 @@ function pick<T>(props: HypostyleObject, theme: Theme): { styles: HypostyleObjec
function createNano(options: { addons?: any[]; prefix?: string }) {
var nano = create({
pfx: options.prefix || '_',
/* c8 ignore next 2 */
// @ts-expect-error
sh: typeof document === 'object' ? document.getElementById('hypo') : null,
})
Expand Down Expand Up @@ -279,6 +284,11 @@ export function hypostyle(
pick: function <T = UnknownKeyValue>(props: HypostyleObject & UnknownKeyValue) {
return pick<T>(props, t)
},
createGlobal: function (props: HypostyleObject) {
var n = createNano({ addons })
n.global(style(explode(obj(props, t), t), t))
return n.raw
},
injectGlobal: function (props: HypostyleObject) {
nano.global(style(explode(obj(props, t), t), t))
},
Expand Down

0 comments on commit 6dc9855

Please sign in to comment.