Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cuddly-carpets-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@component-driven/with-selector': minor
---

Make it possible to use complex selectors like `:active:not([aria-disabled="true"])`
13 changes: 11 additions & 2 deletions packages/with-selector/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ const Button = styled('button')`
border-color: #333;
background: #888;
color: #fff;

&:not([aria-disabled='true']) {
background: cadetblue;
border-color: darkblue;
color: #f5f5f5;
}
}

&.class-name {
&.custom-class {
background: green;
}
`
Expand All @@ -44,7 +50,10 @@ const Button = styled('button')`
<WithSelector selector=":active">
<Button>active</Button>
</WithSelector>
<WithSelector selector=".class-name">
<WithSelector selector={`:active:not([aria-disabled="true"])`}>
<Button>active:not([aria-disabled="true"])</Button>
</WithSelector>
<WithSelector selector=".custom-class">
<Button>class-name</Button>
</WithSelector>
</>
Expand Down
3 changes: 3 additions & 0 deletions packages/with-selector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
"license": "MIT",
"publishConfig": {
"access": "public"
},
"dependencies": {
"nanoid": "^3.1.7"
}
}
15 changes: 12 additions & 3 deletions packages/with-selector/src/WithSelector.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { cloneElement, useEffect, useRef, useState } from 'react'
import { customAlphabet } from 'nanoid'

function addStylesheetRule(rule) {
const styleEl = document.createElement('style')
Expand All @@ -7,19 +8,27 @@ function addStylesheetRule(rule) {
styleSheet.insertRule(rule, styleSheet.cssRules.length)
}

const generateCssClassName = customAlphabet(
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
32
)

// Inspired by https://codesandbox.io/s/pseudo-class-sticker-sheet-jiu2x
const useAddSelector = (ref, selector) => {
const [modifiedClassName, setModifiedClassName] = useState('')
useEffect(() => {
const className = ref.current.classList[ref.current.classList.length - 1]
const fullSelector = `${className && `.${className}`}${selector}`
const classNameWithSelector = fullSelector.replace(/(.)(:|\.)/g, '$1-')
// NOTE: This could be improved, because checking the provided selector starts with a '.'
// is probably not the best way to determine the selector is a class name or not.
const isClassNameSelector = selector.startsWith('.')
let newRule = ''
for (const ss of document.styleSheets) {
for (const rule of ss.cssRules) {
if (fullSelector === rule.selectorText) {
newRule = `${classNameWithSelector} { ${rule.style.cssText}}`
setModifiedClassName(classNameWithSelector.substring(1))
const cssClassName = isClassNameSelector ? selector : `.${generateCssClassName()}`
newRule = `${cssClassName} { ${rule.style.cssText}}`
setModifiedClassName(cssClassName.substring(1))
break
}
}
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5731,6 +5731,11 @@ nan@^2.12.1:
resolved "https://registry.yarnpkg.com/nan/-/nan-2.14.1.tgz#d7be34dfa3105b91494c3147089315eff8874b01"
integrity sha512-isWHgVjnFjh2x2yuJ/tj3JbwoHu3UC2dX5G/88Cm24yB6YopVgxvBObDY7n5xW6ExmFhJpSEQqFPvq9zaXc8Jw==

nanoid@^3.1.7:
version "3.1.7"
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.1.7.tgz#3705ccf590b6a51fbd1794fcf204ce7b5dc46c01"
integrity sha512-ruOwuatdEf4BxQmZopZqhIMudQ9V83aKocr+q2Y7KasnDNvo2OgbgZBYago5hSD0tCmoSl4flIw9ytDLIVM2IQ==

nanomatch@^1.2.9:
version "1.2.13"
resolved "https://registry.yarnpkg.com/nanomatch/-/nanomatch-1.2.13.tgz#b87a8aa4fc0de8fe6be88895b38983ff265bd119"
Expand Down