Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: insertMethod #456

Merged
merged 3 commits into from
Mar 8, 2021
Merged

feat: insertMethod #456

merged 3 commits into from
Mar 8, 2021

Conversation

jonathantneal
Copy link
Contributor

@jonathantneal jonathantneal commented Mar 8, 2021

This PR adds the following:

  • Added an insertMethod feature that lets users override the default DOM insertion method. By default, a stylesheet will prepend it to the beginning of the document head. Passing append changes the insertion method to append it to the end of the document head.

Passing an init function gives the author full control over the dom insertion, which is useful for ShadowDOM wiring up Stitches for a ShadowDOM.

createCss{
  // recreates the default insertion method
  insertMethod(init) => {
    const isAppend = init.insertMethod === 'append'

    let currentCssHead = null
    let currentCssNode = null

    return (cssText) => {
      if (typeof document === 'object') {
        if (!currentCssHead) currentCssHead = document.head || document.documentElement
        if (!currentCssNode) currentCssNode = document.getElementById('stitches') || Object.assign(document.createElement('style'), { id: 'stitches' })
        if (!currentCssNode.parentNode) currentCssHead[isAppend ? 'append' : 'prepend'](currentCssNode)

        currentCssNode.textContent = cssText
      }
    }
  }
})

Resolves #357

@codesandbox-ci
Copy link

codesandbox-ci bot commented Mar 8, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit bfb9c27:

Sandbox Source
Stitches CI: CRA Configuration
Stitches CI: Next.js Configuration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Integrating stitches in a lib, with the possibility of this lib being used in an app using stitches.
1 participant