์๊ณ ๋ผ์คํ ์ด์ธ ๋ฐ๋ชจ ๋ณด๊ธฐ
ํ์ ๊ณผ์ ๋ ์๋์์ง๋ง, ๋ฐ๋ฐํ ํ๋ฉด์ ์ปค๋ฒํด์ค ๋คํฌ๋ชจ๋๋ฅผ ๊ตฌํํด ๋ณด์๋ค.
CSS ๊ณต๋ถํ ๋ ๋ฐฐ์ ๋ CSS variable์ ์จ๋ณด๊ณ ์ถ์ด์ ๋๊ฐ์ง ๋ฐฉ๋ฒ์ผ๋ก ์๋ํด ๋ณด์๋ค.
๋๋ฐ์ด์ค์ ์์คํ
๋คํฌ ๋ชจ๋๋ฅผ ์ ์ฉํ๋ ๊ฒ์ ์๋๋ค!
(::TODO:: ์ถํ์ ๊ธฐํ๊ฐ ์์ผ๋ฉด)
body
ํ๊ทธ์ attribute ์ถ๊ฐbutton
element๋ฅผ ์ถ๊ฐํด ์ค๋ค.body
ํ๊ทธ์ data-theme
attribute๋ฅผ ์ถ๊ฐํ๋ค. (light-mode
/dark-mode
ํ ๊ธ๋ก ์ ์ด)<body data-theme="light-mode">
<!-- ์ดํ ์ค๋ต -->
<div class="darkmode">
<button class="darkmode__btn"></button>
</div>
<!-- ์ดํ ์๋ต -->
[data-theme=โdark-modeโ]
์ ํ์ ์๋ ๋์๋ค./* ๋คํฌ๋ชจ๋์ */
[data-theme="dark-mode"] {
background-color: #111;
color: #fff;
}
/* ๋คํฌ๋ชจ๋์ ๋ฒํผ ์ด๋ฏธ์ง ๋ณ๊ฒฝ */
[data-theme="dark-mode"] .darkmode__btn {
background: url(./img/light-toggle.png) 50% 50% / 30px auto no-repeat;
}
[data-theme="dark-mode"] .form__container {
background-color: slateblue;
border: 1px solid slateblue;
}
/* ์ดํ ์๋ต */
ํ์ฌ body
ํ๊ทธ์ data-theme
attribute๋ฅผ ํ์ธํ๊ณ ์ ์ดํ๊ธฐ ์ํด์ dataset
์ ์ฌ์ฉํ๋ค.
const darkmodeBtn = document.querySelector('.darkmode__btn')
darkmodeBtn.addEventListener('click', () => {
if (document.body.dataset.theme === 'light-mode') {
document.body.dataset.theme = 'dark-mode'
} else {
document.body.dataset.theme = 'light-mode'
}
})
// regExp์ ์ฌ์ฉํ๋ match ๋ฉ์๋๋ฅผ ์ฌ์ฉํด๋ ์ ์ฉ์ด ๋๋๊ตฌ๋..?
darkmodeBtn.addEventListener('click', () => {
let dataAttr = document.body.dataset
dataAttr.theme.match('light-mode')?dataAttr.theme = 'dark-mode':dataAttr.theme = 'light-mode'
})
์ฐ์ Can I use | css-variables์์ css var ํธํ์ฑ ์ฒดํฌ!!
96.51%๋ก ๊ฐ๋ฅํ๋ค๊ณ ๋ด๋ ๋ ๊ฒ ๊ฐ๋ค!
body[data-theme] ์์ฑ์ผ๋ก var๋ฅผ ์ ์ํด ์ฃผ์๋ค. ํ์ผ์ ๋ฐ๋ก ๋ถ๋ฆฌํด๋ ์ข์ ๊ฒ ๊ฐ๋ค.
์ดํ ์์ฑ๊ฐ์ var(โtext)
์ด๋ ๊ฒ ์ ์ฉํ๋ค.
body[data-theme='light-mode'] {
--text: #111;
--line: #eee;
--background: #fff;
--container-background: palegreen;
--container-border: yellowgreen;
--form-button: blueviolet;
--form-button-text: #fff;
--list-background: #fff;
--page-background:blueviolet;
--page-text: #fff;
}
body[data-theme='dark-mode'] {
--text: #fff;
--line: #333;
--background: #151515;
--container-background: slateblue;
--container-border: slateblue;
--form-background: #fff;
--form-button: yellow;
--form-button-text: #111;
--list-background: #202020;
--page-background: yellow;
--page-text: #333;
}
/* ์๋ฅผ ๋ค๋ฉด ์ด๋ ๊ฒ */
.form__submit > input {
width: 100%;
background-color: var(--form-button);
color: var(--form-button-text);
}
๋ณ์๋ฅผ element ๊ธฐ์ค์ผ๋ก ์ ํ๋ค๋ณด๋, ์ฝํ ์ธ ์์ ๋นํด์ ์ ์ธํ ๋ณ์๊ฐ ๋ง์๋ค. ํ ์คํธ, ๋ฐฐ๊ฒฝ, ๋ณด๋ ๋ฑ. ํ์ง๋ง ์ ๋ฐฉ๋ฒ๋ณด๋ค๋ ํจ์ฌ ๊ฐ๊ฒฐํ๊ณ ๋ชจ์๋์์ ๊ด๋ฆฌํ๊ธฐ ์ฌ์ด ๊ฒ ๊ฐ๋ค.
๋ฒจ๋ก๊ทธ์ ๋คํฌ ๋ชจ๋ ์ ์ฉํ๊ธฐ | velopert
can I use | css-variables