How to use the @emotion/sheet.StyleSheet function in @emotion/sheet

To help you get started, we’ve selected a few @emotion/sheet examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github emotion-js / next / packages / sheet / __tests__ / index.js View on Github external
it("should use the container option instead of document.head to insert style elements into if it's passed", () => {
    const container = document.createElement('div')
    // $FlowFixMe
    document.body.appendChild(container)
    const sheet = new StyleSheet({ container })
    expect(sheet.container).toBe(container)
    sheet.insert(rule)
    expect(document.querySelector('html')).toMatchSnapshot()
    expect(sheet.tags).toHaveLength(1)
    expect(sheet.tags[0].parentNode).toBe(container)
    sheet.flush()
    // $FlowFixMe
    document.body.removeChild(container)
  })
})
github emotion-js / emotion / packages / sheet / __tests__ / index.js View on Github external
it('should not throw an error when inserting a @import rule in speedy when a rule has already been inserted', () => {
    const sheet = new StyleSheet({ ...defaultOptions, speedy: true })
    sheet.insert('h1 {color:hotpink;}')
    let importRule =
      "@import url('https://fonts.googleapis.com/css?family=Merriweather');"
    sheet.insert(importRule)
    expect(sheet.tags).toHaveLength(1)
    // $FlowFixMe
    expect(sheet.tags[0].sheet.cssRules[0]).toBeInstanceOf(window.CSSImportRule)
    sheet.flush()
  })
})
github emotion-js / emotion / packages / sheet / types / tests.ts View on Github external
key: 'abc',
  container: document.createElement('div'),
  speedy: true
})
// $ExpectError
new StyleSheet({
  container: document.createElement('div'),
  key: 120
})
new StyleSheet({
  container: document.createElement('div'),
  // $ExpectError
  kye: 'abc'
})

const styleSheet0 = new StyleSheet({
  key: 'abc',
  container: document.createElement('div')
})
const styleSheet1: StyleSheet = styleSheet0
const styleSheet2: StyleSheet = new StyleSheet()

const styleSheet = new StyleSheet({
  key: 'abc',
  container: document.createElement('div')
})

styleSheet.insert('.name{ color: black; }')
styleSheet.insert('.cl{ width: 200px; height: 200px; }')
// $ExpectError
styleSheet.insert()
// $ExpectError
github emotion-js / next / packages / cache / src / index.js View on Github external
// $FlowFixMe
      attrib.split(' ').forEach(id => {
        inserted[id] = true
      })
      // $FlowFixMe
      let parent = options.container || document.head
      if (node.parentNode !== parent) {
        // $FlowFixMe
        parent.appendChild(node)
      }
    })
  }
  const context: CSSContextType = {
    stylis,
    key,
    sheet: new StyleSheet({
      key,
      container: options.container,
      nonce: options.nonce
    }),
    inserted,
    registered: {},
    theme: {}
  }
  return context
}
github emotion-js / emotion / packages / core / src / global.js View on Github external
componentDidMount() {
    this.sheet = new StyleSheet({
      key: `${this.props.cache.key}-global`,
      nonce: this.props.cache.sheet.nonce,
      container: this.props.cache.sheet.container
    })
    // $FlowFixMe
    let node: HTMLStyleElement | null = document.querySelector(
      `style[data-emotion-${this.props.cache.key}="${
        this.props.serialized.name
      }"]`
    )

    if (node !== null) {
      this.sheet.tags.push(node)
    }
    if (this.props.cache.sheet.tags.length) {
      this.sheet.before = this.props.cache.sheet.tags[0]
github emotion-js / emotion / packages / core / src / global.js View on Github external
componentDidMount() {
    this.sheet = new StyleSheet({
      key: `${this.props.cache.key}-global`,
      nonce: this.props.cache.sheet.nonce,
      container: this.props.cache.sheet.container
    })
    // $FlowFixMe
    let node: HTMLStyleElement | null = document.querySelector(
      `style[data-emotion-${this.props.cache.key}="${
        this.props.serialized.name
      }"]`
    )

    if (node !== null) {
      this.sheet.tags.push(node)
    }
    if (this.props.cache.sheet.tags.length) {
      this.sheet.before = this.props.cache.sheet.tags[0]
github emotion-js / next / packages / core / src / global.js View on Github external
renderChild = (context: CSSContextType) => {
    const serialized = serializeStyles(context.registered, [this.props.css])
    if (this.oldName !== serialized.name) {
      if (isBrowser) {
        this.sheet = new StyleSheet({
          key: `${context.key}-global`,
          // $FlowFixMe
          nonce: context.sheet.nonce,
          container: context.sheet.container
        })
      }
      this.oldName = serialized.name
      let rules = context.stylis(``, serialized.styles)
      if (shouldSerializeToReactTree) {
        this.serialized = rules.join('')
      }

      if (isBrowser) {
        this.sheet.flush()
        rules.forEach(rule => {
          this.sheet.insert(rule)
github emotion-js / next / packages / sheet / __tests__ / index.js View on Github external
it('should set the nonce option as an attribute to style elements', () => {
    let nonce = 'some-nonce'
    const sheet = new StyleSheet({ nonce })
    sheet.insert(rule)
    expect(sheet.tags[0]).toBe(document.querySelector('[data-emotion]'))
    expect(sheet.tags).toHaveLength(1)
    expect(sheet.tags[0].getAttribute('nonce')).toBe(nonce)
    sheet.flush()
  })
github emotion-js / emotion / packages / sheet / __tests__ / index.js View on Github external
it('should insert a rule into the DOM when not in speedy', () => {
    const sheet = new StyleSheet(defaultOptions)
    sheet.insert(rule)
    expect(document.documentElement).toMatchSnapshot()
    sheet.flush()
  })
github emotion-js / next / packages / sheet / __tests__ / index.js View on Github external
it('should remove its style elements from the document when flushed', () => {
    const sheet = new StyleSheet()
    sheet.insert(rule)
    expect(document.querySelector('html')).toMatchSnapshot()
    sheet.flush()
    expect(document.querySelector('html')).toMatchSnapshot()
  })

@emotion/sheet

emotion's stylesheet

MIT
Latest version published 5 months ago

Package Health Score

83 / 100
Full package analysis