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

Fix d.style being undefined and code golf #108

Merged
merged 1 commit into from
May 4, 2022

Conversation

shuding
Copy link
Contributor

@shuding shuding commented May 4, 2022

When using attribute: 'class', the inserted script tag will be:

<script>
!function(){try {
  var d=document.documentElement.classList;
  d.remove('light','dark');
  var e=localStorage.getItem('theme');
  if("system"===e||(!e&&true)){
    var t="(prefers-color-scheme: dark)",m=window.matchMedia(t);
    if(m.media!==t||m.matches){
      d.style.colorScheme = 'dark';
      d.add('dark')
    }else{
      d.style.colorScheme = 'light';
      d.add('light')}
    }else if(e){
      d.add(e|| '')
    }
    if(e==='light'||e==='dark')
      d.style.colorScheme=e
  }catch(e){}
}()
</script>

...where d=document.documentElement.classList but later it tries to access its style property via d.style.colorScheme. Should be using document.documentElement here instead.

This PR changes it to explicitly use d for document.documentElement and c for d.classList.

Also removes a couple of bytes.

@vercel
Copy link

vercel bot commented May 4, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
next-themes-basic ✅ Ready (Inspect) Visit Preview May 4, 2022 at 8:08PM (UTC)
next-themes-tailwind ✅ Ready (Inspect) Visit Preview May 4, 2022 at 8:08PM (UTC)

@pacocoursey pacocoursey merged commit 7bcb4ed into pacocoursey:0.2 May 4, 2022
@shuding shuding deleted the patch-1 branch May 5, 2022 10:34
pacocoursey added a commit that referenced this pull request May 5, 2022
* pass script as children instead of dangerouslySetInnerHTML or base64 encoding

* v0.1.2-beta.0

* return script element directly instead of putting in head

* fix `d.style` being undefined and code golf (#108)

* add children type to themeproviderprops

* bump package versions to latest, ensure next@12.1.6 is used

* latest microbundle, fix types

* standardize quote

* fix hydration warning in example

* fix package exports

* fix the types another way

* change peerdep version of next back to any

* publish beta

* beta

* remove accidental export

Co-authored-by: Shu Ding <g@shud.in>
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.

2 participants