그러나 테마나 플러그인이 SCSS 색상 함수를 사용하여 색상을 어둡게 하거나 밝게 하는 등 더 복잡한 색상 변형을 사용하는 경우가 있습니다. 이러한 경우 더 복잡한 리팩터링이 필요하며, 이를 위해 테마와 플러그인에서 색상 정의를 확장할 수 있는 기능을 추가했습니다.
플러그인에서
discourse-encrypt 플러그인의 이 커밋은 이러한 리팩터링의 우수하고 간단한 예시입니다. 이 커밋은 mix($color1, $color2) SCSS 선언을 별도의 파일로 이동시키고 이를 CSS 커스텀 프로퍼티로 저장합니다. 그런 다음 새 파일이 :color_definitions 자산으로 등록되어 새로 선언된 색상 프로퍼티가 색상 정의 스타일시트에 포함되도록 보장합니다.
테마에서
테마에서는 common/color_definitions.scss 스타일시트에 CSS 커스텀 프로퍼티를 선언하여 동일한 작업을 수행할 수 있습니다. 예시로 graceful 테마의 이 커밋을 참고할 수 있습니다.
추가 참고 사항
rgba($color, 0.5) 함수를 통해 투명 색상을 사용할 때, SCSS는 첫 번째 매개변수로 HEX 및 RGB 색상을 허용하지만 CSS 커스텀 프로퍼티는 RGB 색상만 허용합니다. 이러한 이유로 색상 정의에 hexToRGB() 헬퍼와 --rgb 접미사가 붙은 일부 프로퍼티를 도입했습니다. 예시:
위 스니펫에서 SCSS 변수가 커스텀 프로퍼티로 전달될 때 보간(interpolation)된다는 점에 유의하세요. 이는 SCSS의 요구 사항이며, 자세한 내용은 Sass: Property Declarations 를 참조하세요.
CSS의 var() 선언은 첫 번째 값이 사용 불가능한 경우 두 번째 값으로 폴백할 수 있습니다. 예를 들어 var(--color1, red)를 작성하면 --color1 프로퍼티가 발견되지 않으면 CSS가 빨간색으로 폴백합니다. 플러그인에서는 이전 버전의 Discourse와의 호환성을 보장하기 위해 SCSS 색상 변수를 폴백으로 사용합니다. 따라서 이전 예시는 폴백이 포함된 경우 다음과 같이 보입니다:
I’m not very good at this stuff and it’ll take me a while to figure this out myself. . . Does this mean that all themes that referred to colors before are now going to be broken?
No, not at all. SCSS variables in themes will continue to work for a long time.
But any colors outputted via SCSS variables will stay static, i.e. they cannot be dynamically switched to a new color scheme when a browser goes from normal to dark mode. So those themes/plugins will continue to work, they just won’t be compatible with automatic dark mode switching.
Thanks for the instructions. Is there a way to also change a background image depending on dark/light mode? (I’ve used the theme switcher component to do that.) Would a CSS class indicating the mode be possible?
Great question, I tried this and noticed that we didn’t properly support using background images or theme variables in the special color definitions stylesheet. So I made some fixes in core, and you should be able to do this now (make sure you pull the latest core).
So, if you have two images in your theme or theme component, with SCSS vars of $bg-light and $bg-dark respectively, you can add this to your color_definitions.scss stylesheet:
That won’t work well in all cases, because the media query is not aware of the user’s preferences. Users can disable auto-dark-mode switching, but the media query won’t be aware of that, and it will result in the background meant for the dark color scheme being rendered.
우리의 색상 스킴에는 --scheme-type이라는 프로퍼티가 포함되어 있으며, 라이트 스킴의 경우 light, 다크 스킴의 경우 dark 값이 지정됩니다. 최신 브라우저들은 컨테이너 쿼리를 지원하므로, 페이지 body에 클래스를 추가하지 않고도 여기서 시도하려는 작업을 수행할 수 있습니다.