선택자

의사 클래스 열에 정의된 속성은 선택자 열의 이름과 대응하므로, 컴포넌트 props로 바로 사용할 수 있습니다.


선택자의사 클래스
_active:active
_checked:checked
_default:default
_disabled:disabled
_empty:empty
_enabled:enabled
_first

:first (@page 의사 클래스입니다. 첫 번째 자식과는 일치하지 않으며, 첫 번째 자식에는 _firstChild를 사용합니다)

_firstChild:first-child
_firstOfType:first-of-type
_focus:focus
_focusVisible:focus-visible
_focusWithin:focus-within
_hover:hover
_invalid:invalid
_lastChild:last-child
_lastOfType:last-of-type
_link:link
_onlyChild:only-child
_optional:optional
_readOnly:read-only

다른 의사 클래스나 의사 요소도 같은 방식으로 동작합니다. 예를 들어 _nthChild는 :nth-child, _placeholder는 ::placeholder에 해당합니다. _notASelector처럼 아무것도 선택하지 않는 이름은 아무것과도 일치하지 않는 CSS를 만드는 대신 빌드 오류가 됩니다.

인자를 받는 의사 클래스는 params로 인자를 전달하며, 그룹 형태에서도 마찬가지입니다.

1const example = (
2  <Box
3    _groupNthChild={{ params: ['odd'], color: 'red' }}
4    _nthChild={{ params: ['2n'], bg: 'gray' }}
5  />
6)
7// :is([role=group],[data-group]):nth-child(odd) .a{color:red}
8// .b:nth-child(2n){background:gray}
1const example = (
2  <Box
3    _groupNthChild={{ params: ['odd'], color: 'red' }}
4    _nthChild={{ params: ['2n'], bg: 'gray' }}
5  />
6)
7// :is([role=group],[data-group]):nth-child(odd) .a{color:red}
8// .b:nth-child(2n){background:gray}

선택자는 스타일을 받습니다. 객체 또는 _hover="color: red" 같은 CSS 텍스트를 사용할 수 있습니다. 클래스 이름처럼 그 밖의 문자열은 빌드 오류가 됩니다.

selectors

selectors는 모든 선택자를 받습니다. &가 있는 키에서는 그 위치에 요소가 들어가고, &가 없는 키는 요소를 기준으로 해석됩니다.

  • hover나 focusVisible 같은 의사 클래스 이름은 요소의 해당 의사 클래스가 됩니다. div처럼 아무것도 선택하지 않는 이름은 빌드 오류입니다
  • :, ::, [로 시작하는 키는 ':hover'나 '[aria-busy]'처럼 요소 자체에 적용됩니다
  • 그 밖의 선택자는 '.child', '> p', 'div p'처럼 하위 요소를 선택합니다
1const example = (
2  <Box
3    selectors={{
4      '&:hover, .parent &': { color: 'red' },
5      '[aria-busy]': { opacity: 0.5 },
6      '> p': { m: 0 },
7    }}
8  />
9)
10// .a:hover{color:red}
11// .parent .b{color:red}
12// .c[aria-busy]{opacity:.5}
13// .d > p{margin:0}
1const example = (
2  <Box
3    selectors={{
4      '&:hover, .parent &': { color: 'red' },
5      '[aria-busy]': { opacity: 0.5 },
6      '> p': { m: 0 },
7    }}
8  />
9)
10// .a:hover{color:red}
11// .parent .b{color:red}
12// .c[aria-busy]{opacity:.5}
13// .d > p{margin:0}

styled와 css 템플릿의 중첩 규칙은 styled-components 및 Emotion과 같이 동작합니다. .child { }와 > p { }는 하위 요소를 선택하고, :hover { }와 ::before { }는 컴포넌트 자체에 적용되며, 선택자 목록 안에 중첩된 규칙은 목록의 각 선택자 아래에 적용됩니다.

스타일 객체의 중첩 선택자

스타일 객체도 Emotion 및 styled-components와 같이 동작합니다. css(), globalCss(), styled.div({ ... }), styled(Component)({ ... }), css prop, Emotion 및 styled-components import에서 값이 객체이고 어떤 스타일 속성도 가리키지 않는 키는 중첩 선택자입니다. selectors 키와 같은 방식으로 해석되지만, 의사 클래스를 선택하지 않는 이름은 빌드 오류 대신 하위 요소 선택자가 됩니다.

  • span 같은 요소 이름이나 'h1, h2' 같은 선택자 목록은 하위 요소를 선택하며, 목록의 선택자마다 규칙이 하나씩 생성됩니다
  • hover, before, print 같은 의사 클래스, 의사 요소, 미디어 축약형 이름은 selectors에서와 마찬가지로 해당 선택자가 됩니다
  • :, ::, [로 시작하는 키는 요소 자체에 적용되며, &는 요소가 들어갈 위치를 나타냅니다
1const title = css({
2  color: 'red',
3  span: { color: 'blue' },
4  'h1, h2': { m: 0 },
5  hover: { color: 'green' },
6  '&.active': { fontWeight: 'bold' },
7})
8// .a{color:red}
9// .b span{color:blue}
10// .c h1{margin:0}
11// .d h2{margin:0}
12// .e:hover{color:green}
13// .f.active{font-weight:bold}
14
15globalCss({ body: { color: 'black', a: { color: 'blue' } } })
16// body{color:black}
17// body a{color:blue}
1const title = css({
2  color: 'red',
3  span: { color: 'blue' },
4  'h1, h2': { m: 0 },
5  hover: { color: 'green' },
6  '&.active': { fontWeight: 'bold' },
7})
8// .a{color:red}
9// .b span{color:blue}
10// .c h1{margin:0}
11// .d h2{margin:0}
12// .e:hover{color:green}
13// .f.active{font-weight:bold}
14
15globalCss({ body: { color: 'black', a: { color: 'blue' } } })
16// body{color:black}
17// body a{color:blue}

스타일 속성인 키는 값이 객체여도 원래 의미를 유지합니다. p는 Devup UI의 padding 축약형이므로 p: { ... }는 문단을 선택하지 않습니다. 문단을 선택하려면 '& p'를 작성합니다. 철자가 틀린 colr: 'red'처럼 문자열 값을 가진 키는 선언으로 유지됩니다. JSX props는 스타일 객체가 아닙니다. <Box span={...} />는 이전과 같이 span을 요소에 전달하므로, 여기에서 선택자를 중첩하려면 selectors나 css prop을 사용합니다.

미디어 축약형

다음 props는 스타일을 고정된 미디어 쿼리로 감쌉니다. 반응형 배열을 받으며, 선택자 및 다른 at-rule과 어느 방향으로든 중첩할 수 있습니다.


선택자미디어 쿼리
_printprint
_screenscreen
_allall
_portrait(orientation:portrait)
_landscape(orientation:landscape)
_motionReduce(prefers-reduced-motion:reduce)
_motionSafe(prefers-reduced-motion:no-preference)
_contrastMore(prefers-contrast:more)
_contrastLess(prefers-contrast:less)
_forcedColors(forced-colors:active)
1const example = (
2  <Box
3    _motionReduce={{ transition: 'none' }}
4    _motionSafe={{ _hover: { transform: 'scale(1.05)' } }}
5    transition={['opacity .2s', null, 'all .3s']}
6  />
7)
1const example = (
2  <Box
3    _motionReduce={{ transition: 'none' }}
4    _motionSafe={{ _hover: { transform: 'scale(1.05)' } }}
5    transition={['opacity .2s', null, 'all .3s']}
6  />
7)

At-Rule

_media, _supports, _container는 쿼리를 키로, 스타일을 값으로 하는 레코드를 받습니다. Emotion처럼 쿼리를 키 자체에 작성할 수도 있으며, globalCss는 선택자 맵 전체를 감싸는 at-rule을 지원합니다.

1const example = (
2  <Box
3    _media={{ '(min-width: 500px)': { p: 2 } }}
4    selectors={{ '@media print': { display: 'none' } }}
5  />
6)
7
8globalCss({
9  _motionReduce: { '*, *::before, *::after': { transition: 'none' } },
10})
1const example = (
2  <Box
3    _media={{ '(min-width: 500px)': { p: 2 } }}
4    selectors={{ '@media print': { display: 'none' } }}
5  />
6)
7
8globalCss({
9  _motionReduce: { '*, *::before, *::after': { transition: 'none' } },
10})

조건은 항상 브레이크포인트 값보다 우선합니다. at-rule 스타일은 모든 반응형 규칙 뒤에 옵니다. at-rule끼리는 조건에 따라 정렬되며, 너비 쿼리는 모바일 우선(min-width 오름차순, 그다음 max-width 내림차순)으로, 그 뒤에 미디어 타입, 기타 기능 쿼리 순이고, prefers-* 같은 사용자 환경 설정은 마지막에 옵니다.