Giter VIP home page Giter VIP logo

Comments (3)

B-Teague avatar B-Teague commented on May 23, 2024

Another option is just use a counter instead of generated hash names like picostyle.
csz-1, csz-2, csz-3, etc.
This provides unique class names if the user doesn't care about class names and is more efficient than the suggested solution above.

If developers do care about class names, then a solution to issue #6 needs to be implemented

from csz.

lukejacksonn avatar lukejacksonn commented on May 23, 2024

Random is hard 😅 the chance of collision is pretty minute here although you make a good point about replacing (or prefixing I guess) the random hash with a incrementing integer.

This kinda feels like a over optimisation as Math.random().toString(36).replace('0.', '') should be sufficient in most cases. If you are creating enough unique styles to start worrying about perf degradation and collisions then you have bigger things to worry about!

from csz.

B-Teague avatar B-Teague commented on May 23, 2024

Yup, definitely over optimizing. I like math =)
I think for both efficiency and transparency, the best solution would be a counter and custom css class names.

import stylis from './stylis.js'

const cache = {}
+let csz = 0
-const hash = () =>
-  Math.random()
-    .toString(36)
-    .replace('0.', '')

const sheet = document.createElement('style')
document.head.appendChild(sheet)

-const none = hash => `.${hash}{display:none}`
+const none = className => `.${className}{display:none}`
-const hide = hash => (sheet.innerHTML = none(hash) + sheet.innerHTML)
+const hide = className => (sheet.innerHTML = none(className) + sheet.innerHTML)
-const show = hash => (sheet.innerHTML = sheet.innerHTML.replace(none(hash), ''))
+const show = className => (sheet.innerHTML = sheet.innerHTML.replace(none(className), ''))

-const process = key => hash => rules => {
+const process = key => className => rules => {
-  if (key.startsWith('/')) show(hash)
+  if (key.startsWith('/')) show(className)
  sheet.innerHTML += (cache[key] = {
-    hash,
+    className,
-    rules: stylis()(`.${hash}`, rules),
+    rules: stylis()(`.${className}`, rules),
  }).rules
}

export default (strings, ...values) => {
+ //csz`.my-class { background: blue; }`
+ const classPrefix= strings[0].startsWith('.') ? strings[0].substring(1, strings[0].indexOf(' ')) : "csz-"
  const key = strings[0].startsWith('/')
    ? strings[0]
    : strings.reduce(
        (acc, string, i) =>
          (acc += string + (values[i] == null ? '' : values[i])),
        ''
      )

-  if (cache[key]) return cache[key].hash
+  if (cache[key]) return cache[key].className

-  const className = 'csz-' + hash()
+  const className = (classPrefix.charAt(--classPrefix.length) === '-' ? classPrefix : classPrefix + '-') + csz++
  const append = process(key)(className)

  if (key.startsWith('/')) {
    hide(className)
    fetch(key)
      .then(res => res.text())
      .then(append)
+ } else if (key.startsWith('.')) {
+   // Below supports both csz`.my-class { background: blue; }` Or csz`.my-class background: blue;`
+   const ruleStart = key.indexOf(' ') === key.replace(' ', '').indexOf('{') ? key.indexOf('{') : key.indexOf(' ')
+   const ruleEnd = key.trim().lastIndexOf('}') === key.length - 1 ? key.lastIndexOf('}') : key.length
+   apend(key.substring(ruleStart + 1, ruleEnd))
  } else append(key)

  return className
}

from csz.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.