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

Utility Groups (Why not?!) #9

Open
deleonio opened this issue Apr 10, 2021 · 11 comments
Open

Utility Groups (Why not?!) #9

deleonio opened this issue Apr 10, 2021 · 11 comments

Comments

@deleonio
Copy link
Contributor

That is currently not possible?!

class="hover:(border-green-600 shadow-lg)"

We give the parser the files and he extract the css classes. Why he did not detect the utility groups?

@antfu
Copy link
Member

antfu commented Apr 10, 2021

It requires transformations to the source file, not just serving the css. Which I don't think it's capable for postcss's plugin interface.

@deleonio
Copy link
Contributor Author

Is it possible to push the css as string instead of a file path?

@antfu
Copy link
Member

antfu commented Apr 10, 2021

Sorry what do you mean?

@deleonio
Copy link
Contributor Author

Sorry, the extractFile function receive the css as string and should make the group handling too.

https://github.com/windicss/vite-plugin-windicss/blob/ce166a0334f1a36e9d24513caf391a53585ac249/packages/plugin-utils/src/createUtils.ts#L161

But it does not work in my case.

@deleonio
Copy link
Contributor Author

deleonio commented Apr 10, 2021

🤦 Embarrassing, now I understand it too. The detection works.

image

🤔 ... I will think about it.

@deleonio
Copy link
Contributor Author

Hello @antfu,

I implemented a tiny devtool script that transforms the class declaration like Windi under the hood.

But today I have no idea how to dynamically insert it into the development code.

// windi stuff
const regexClassGroup = /([!\w+-][\w+:_/-]*?\w):\(([\w\s/-]*?)\)/gm;
function transformGroups(str: string) {
  return str.replace(regexClassGroup, (_, a: string, b: string) =>
    b
      .split(/\s/g)
      .map((i) => `${a}:${i}`)
      .join(' ')
  );
}

// devtool
document.addEventListener('DOMNodeInserted', (event: MutationEvent) => {
  const nodes: NodeListOf<HTMLElement> = event.relatedNode.querySelectorAll('[class]');
  nodes.forEach((node: HTMLElement) => {
    const transformed = transformGroups(node.getAttribute('class') || '');
    if (node.getAttribute('class') != transformed) {
      node.setAttribute('class', transformed);
    }
  });
});

image

@antfu
Copy link
Member

antfu commented Apr 12, 2021

Oh, like a runtime?

@deleonio
Copy link
Contributor Author

I could try to inject the devtool snippet only in development mode.

@deleonio
Copy link
Contributor Author

deleonio commented Apr 19, 2021

Update prototype - valid W3C - https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver

const regexClassGroup = /([!\w+-][\w+:_/-]*?\w):\(([\w\s/-]*?)\)/gm;
function transformGroups(str: string) {
  return str.replace(regexClassGroup, (_, a: string, b: string) =>
    b
      .split(/\s/g)
      .map((i) => `${a}:${i}`)
      .join(' ')
  );
}

new MutationObserver((records: MutationRecord[]) => {
  records.forEach((record: MutationRecord) => {
    record.addedNodes.forEach((node: Node) => {
      const nodes: NodeListOf<HTMLElement> | undefined = node.parentElement?.querySelectorAll('[class]');
      if (nodes) {
        nodes.forEach((node: HTMLElement) => {
          const transformed = transformGroups(node.getAttribute('class') || '');
          if (node.getAttribute('class') != transformed) {
            node.setAttribute('class', transformed);
          }
        });
      }
    });
  });
}).observe(document.body, {
  attributes: true,
  childList: true,
  subtree: true,
});

@antfu
Copy link
Member

antfu commented Apr 19, 2021

Interesting, do you think we can make a package called windicss-groups-runtime-polyfill or something so people can opt-in by simply importing it via npm or CDN.

/cc @voorjaar do you feel we can make this one official?

@George-Miao
Copy link

Hi! Is this usable now? Really wanted to have variant grouping.

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

No branches or pull requests

3 participants