အားလုံးပဲ မင်္ဂလာပါ။
ညီမကတော့ Spiceworks Myanmar မှာ Web Designer အဖြစ် တာဝန်ယူလုပ်ကိုင်နေတဲ့ နီလာဌေး ဖြစ်ပါတယ်။ ဒီနေ့မှာတော့ pre-processor scripting languages တွေထဲမှာ popular ဖြစ်တဲ့ Sass အကြောင်းကို လေ့လာထားသလောက် ပြန်လည်ဝေမျှပေးချင်ပါတယ်။
Sass Introduction
Sass က CSS pre-processor တစ်ခုဖြစ်ပါတယ်။ နောက်ပြီး CSS ကို ထပ်ခါထပ်ခါရေးခြင်းမှ လျှော့ချပေးနိုင်လို့ အချိန်ကုန်လည်း သက်သာစေပါတယ်။ Sass မလေ့လာခင် အခြေခံအားဖြင့် HTML, CSS တို့ကိုတော့ သိထားဖို့ လိုအပ်ပါတယ်။
What is Sass?
Sass stands for Syntactically Awesome Stylesheet and is an extension to CSS.
Sass ကို 2006 ခုနှစ်မှာ Hampton Catlin က design လုပ်ခဲ့ပြီး Natalie Weizenbaum က develop လုပ်ခဲ့တာပဲ ဖြစ်ပါတယ်။
Why Use Sass?
Stylesheets တွေက ကြီးလာပြီး ရှုပ်ထွေးလာတယ်။ နောက်ပြီး maintain လုပ်ဖို့ ခက်ခဲလာတယ်။ အဲ့ဒီပြဿနာတွေအတွက် CSS pre-processor (Sass) က ကူညီပေးနိုင်တယ်။ CSS မှာမပါတဲ့ nesting, mixins, inheritance, maintainable CSS စတဲ့ features တွေကိုလည်း သုံးလို့ရတယ်။
How Does Sass Work?
Sass က browsers အားလုံးမှာတော့ တိုက်ရိုက် support မပေးပေမယ့် browser နားလည်အောင် လုပ်ပေးလို့ ရပါတယ်။ အဲ့ဒီအတွက် Sass code ကို CSS code ပြောင်းပေးဖို့ Sass pre-processor ကို လိုအပ်ပါတယ်။
Sass File Type
၁။ SCSS syntax (.scss)
၂။ Indented syntax (.sass)
File type အနေနဲ့ (၂)မျိုး ရှိပါတယ်။ .scss ကို အများဆုံး အသုံးပြုကြတယ်။ .sass ကတော့ အသုံးပြုတာနည်းတယ်။ ရေးနည်းမှာ curly braces နဲ့ semicolons တွေမပါဝင်ပါဘူး။ ဒါပေမယ့် sample code ပြတဲ့အခါမှာ syntaxes နှစ်မျိုးလုံးနဲ့ ပြပေးသွားပါမယ်။
Sass Installation
Sass က independent platform ဖြစ်ပြီး Programming language အနေနဲ့ Ruby ကို အခြေခံထားပါတယ်။ Install လုပ်တဲ့နည်းလမ်းတွေလည်း အများကြီးရှိပါတယ်။ Sass official website မှာ အသေးစိတ်လေ့လာနိုင်ပါတယ်။
Sass Compiler
ပထမပိုင်းပြောခဲ့သလိုပဲ Sass code ကို CSS code ပြောင်းပေးဖို့အတွက် နည်းလမ်းတွေ အများကြီးရှိပါတယ်။ Command သုံးပြီးတော့လည်း compile လုပ်နိုင်တယ်။ အခု ဒီ blog မှာတော့ VS code ရဲ့ extension တစ်ခုဖြစ်တဲ့ Live Sass Compiler ရဲ့ အသုံးပြုနည်းလေးကို ပြပေးသွားပါမယ်။
ပုံ(၁)မှာ ပြထားတဲ့အတိုင်း install လုပ်နိုင်ပါတယ်။

Compiled လုပ်ပြီး ထွက်လာတဲ့ CSS ဖိုင်ကို ထားမယ့်နေရာအတွက် setup လုပ်ပေးဖို့လိုပါမယ်။ အဲ့ဒီအတွက် ပုံ(၂)မှာ ပြထားတဲ့အတိုင်း settings.json file မှာ ပြင်ပေးရပါမယ်။

ပုံ(၃)မှာ ပြထားတဲ့အတိုင်း ကိုယ်ထားမယ့် ပတ်လမ်းကြောင်းပြင်ပြီး save လိုက်ရင်ရပါပြီ။

Install လုပ်ပြီးသွားပြီဖြစ်တဲ့အတွက် status bar မှာ ပုံ(၄)ကလို Watch Sass ဆိုပြီးပေါ်လာပါလိမ့်မယ်။ အသုံးပြုနည်းကတော့ အဲ့ဒီ Watch Sass ကို click လိုက်ရင်ရပါပြီ။ Stop ချင်ရင်လည်း နောက်တစ်ချက်ပြန် click လိုက်ရင် ရပ်သွားပါလိမ့်မယ်။

ပုံ(၅)မှာ ပြထားတဲ့ sample code အတိုင်း Scss file တစ်ခုဖန်တီးပြီး Watch Sass လုပ်လိုက်မယ်ဆိုရင် ပုံ(၃) မှာ ကိုယ် save လိုက်တဲ့ ပတ်လမ်းကြောင်းအတိုင်း CSS file ကို ထုတ်ပေးပါလိမ့်မယ်။ .css နဲ့ .css.map ဆိုပြီး နှစ်ဖိုင်ထုတ်ပေးပါတယ်။ HTML file မှာ .css file ပဲချိတ်ပေးရင်ရပါတယ်။ Scss file မှာတစ်နေရာပြင်တိုင်း အောက်က output မှာ auto ထုတ်ပေးနေတဲ့အတွက် သုံးရတာ အဆင်ပြေပါတယ်။ ရေးနည်းမှားရင်လည်း error ထုတ်ပေးတဲ့အတွက် code စစ်ပြီးသားလည်း ဖြစ်တယ်။

Sass Variables
Stylesheet မှာ ပြန်လည်အသုံးပြုချင်တဲ့ properties တွေရဲ့ values တွေကို variable အနေနဲ့ ကြေညာပြီး အသုံးပြုလို့ရပါတယ်။ နောက်ပိုင်း update လုပ်ချင်တဲ့အခါမှာ တစ်နေရာထဲမှာပဲ ပြင်ရတဲ့အတွက် အချိန်ကုန်လည်း သက်သာစေပါတယ်။ Variable ကြေညာဖို့အတွက် $ symbol ကိုအသုံးပြုပါတယ်။ အများဆုံး variable ကြေညာတဲ့ properties တွေကတော့ font နဲ့ color တို့ဖြစ်ပါတယ်။
SCSS
1.
$myFont: 'Roboto' , sans-serif;
2.
$mycolor: red;
3.
4.
body {
5.
font-family: $myFont;
6.
color: $mycolor;
7.
}
Sass
1.
$myFont: 'Roboto' , sans-serif
2.
$mycolor: red
3.
4.
body
5.
font-family: $myFont
6.
color: $mycolor
CSS
1.
body {
2.
font-family: "Roboto", sans-serif;
3.
color: red;
4.
}
Sass Nesting
CSS selectors တွေကို HTML လိုပဲ တစ်စုတစ်စည်းတည်း သူ့အဆင့်နဲ့သူ ရှင်းရှင်းလင်းလင်းရေးလို့ရပါတယ်။ Nesting ကို အသုံးပြုရတာ CSS ထက် ရှင်းလင်းပြီး ဖတ်ရတာ လွယ်ကူပါတယ်။
SCSS
01.
nav {
02.
ul {
03.
margin: 0;
04.
padding: 0;
05.
list-style-type: none;
06.
07.
li {
08.
display: inline-block;
09.
10.
a {
11.
display: block;
12.
padding: 6px 12px;
13.
text-decoration: none;
14.
}
15.
}
16.
}
17.
}
Sass
01.
nav
02.
ul
03.
margin: 0
04.
padding: 0
05.
list-style-type: none
06.
07.
li
08.
display: inline-block
09.
10.
a
11.
display: block
12.
padding: 6px 12px
13.
text-decoration: none
CSS
01.
nav ul {
02.
margin: 0;
03.
padding: 0;
04.
list-style-type: none;
05.
}
06.
nav ul li {
07.
display: inline-block;
08.
}
09.
nav ul li a {
10.
display: block;
11.
padding: 6px 12px;
12.
text-decoration: none;
13.
}
Sass Partials
Underscore (_) နဲ့ စတဲ့ file အားလုံးကို partial Sass file လို့ခေါ်ပါတယ်။ File name အစကို underscore (_) နဲ့ပေးမှသာ CSS file မထုတ်ပေးမှာဖြစ်ပါတယ်။ တခြားဖိုင်ကနေ ပြန်ခေါ်သုံးချင်တဲ့အခါမှာ @import ကိုအသုံးပြုပါတယ်။ @use နဲ့လည်းပဲ ခေါ်သုံးလို့ရပါတယ်။ ခေါ်တဲ့အခါမှာ underscore (_) နဲ့ file extension ကို ထည့်ပေးစရာမလိုပါဘူး။
SCSS
01.
// _colors.scss
02.
$myRed: red;
03.
$myGreen: green;
04.
$myBlue: Blue;
05.
06.
// style.scss
07.
@import "colors";
08.
09.
h1 {
10.
color: $myRed;
11.
}
12.
h2 {
13.
color: $myGreen;
14.
}
15.
p {
16.
color: $myBlue;
17.
}
Sass
01.
// _colors.sass
02.
$myRed: red
03.
$myGreen: green
04.
$myBlue: Blue
05.
06.
// style.sass
07.
@import "colors"
08.
09.
h1
10.
color: $myRed
11.
12.
h2
13.
color: $myGreen
14.
15.
p
16.
color: $myBlue
CSS
01.
h1 {
02.
color: red;
03.
}
04.
h2 {
05.
color: green;
06.
}
07.
p {
08.
color: Blue;
09.
}
Sass Modules
Sass ဖိုင်အားလုံးကို ဖိုင်တစ်ခုတည်း ရေးဖို့မလိုအပ်ပါဘူး။ လိုအပ်ရင် modules ထုတ်ပြီး ခွဲရေးလို့ရပါတယ်။ e.g variables, mixins, and functions တွေကို modules အနေနဲ့ ထုတ်ပြီး ကိုယ်အသုံးပြုချင်တဲ့ဖိုင်မှာ @use rule ကိုသုံးပြီး ပြန်ခေါ်သုံးလို့ရပါတယ်။
SCSS
01.
// _base.scss
02.
$myFont: 'Roboto', sans-serif;
03.
$myColor: red;
04.
05.
body {
06.
font-family: $myFont;
07.
}
08.
09.
// style.scss
10.
@use "base";
11.
12.
p {
13.
color: base.$myColor;
14.
}
Sass
01.
// _base.sass
02.
$myFont: 'Roboto', sans-serif
03.
$myColor: red
04.
05.
body
06.
font-family: $myFont
07.
08.
09.
// style.sass
10.
@use "base"
11.
12.
p
13.
color: base.$myColor
CSS
1.
body {
2.
font-family: "Roboto", sans-serif;
3.
}
4.
p {
5.
color: red;
6.
}
Sass Mixins
ပြန်လည်အသုံးပြုချင်တဲ့ styles တွေကို group ဖွဲ့ထားပြီး ကိုယ်အသုံးပြုချင်တဲ့အခါမှာ @include နဲ့ ပြန်ခေါ်သုံးလို့ရပါတယ်။ အဲ့ဒီလိုရေးခြင်းဖြင့် Sass ကို ထပ်ခါထပ်ခါရေးခြင်းကနေ လျှော့ချပေးနိုင်ပါတယ်။
SCSS
01.
@mixin theme($theme: DarkGray) {
02.
background: $theme;
03.
box-shadow: 0 0 1px rgba($theme, .25);
04.
color: #fff;
05.
}
06.
07.
.info {
08.
@include theme;
09.
}
10.
.alert {
11.
@include theme($theme: DarkRed);
12.
}
13.
.success {
14.
@include theme($theme: DarkGreen);
15.
}
Sass
01.
@mixin theme($theme: DarkGray)
02.
background: $theme
03.
box-shadow: 0 0 1px rgba($theme, .25)
04.
color: #fff
05.
06.
.info
07.
@include theme
08.
09.
.alert
10.
@include theme($theme: DarkRed)
11.
12.
.success
13.
@include theme($theme: DarkGreen)
CSS
01.
.info {
02.
background: DarkGray;
03.
box-shadow: 0 0 1px rgba(169, 169, 169, 0.25);
04.
color: #fff;
05.
}
06.
.alert {
07.
background: DarkRed;
08.
box-shadow: 0 0 1px rgba(139, 0, 0, 0.25);
09.
color: #fff;
10.
}
11.
.success {
12.
background: DarkGreen;
13.
box-shadow: 0 0 1px rgba(0, 100, 0, 0.25);
14.
color: #fff;
15.
}
Sass Inheritance
Selector တစ်ခုရဲ့ CSS properties တွေကို တခြား selector တစ်ခုကို share ပေးချင်တဲ့အခါမှာ @extend ကိုခေါ်ပြီး ရေးပါတယ်။
SCSS
01.
%message-shared {
02.
border: 1px solid #ccc;
03.
padding: 10px;
04.
color: #333;
05.
}
06.
07.
.message {
08.
@extend %message-shared;
09.
}
10.
.success {
11.
@extend %message-shared;
12.
border-color: green;
13.
}
14.
.error {
15.
@extend %message-shared;
16.
border-color: red;
17.
}
18.
.warning {
19.
@extend %message-shared;
20.
border-color: yellow;
21.
}
Sass
01.
%message-shared
02.
border: 1px solid #ccc
03.
padding: 10px
04.
color: #333
05.
06.
.message
07.
@extend %message-shared
08.
09.
.success
10.
@extend %message-shared
11.
border-color: green
12.
13.
.error
14.
@extend %message-shared
15.
border-color: red
16.
17.
.warning
18.
@extend %message-shared
19.
border-color: yellow
CSS
01.
.warning, .error, .success, .message {
02.
border: 1px solid #ccc;
03.
padding: 10px;
04.
color: #333;
05.
}
06.
.success {
07.
border-color: green;
08.
}
09.
.error {
10.
border-color: red;
11.
}
12.
.warning {
13.
border-color: yellow;
14.
}
Sass Operators
CSS မှာလိုမျိုး Sass မှာလည်း operators တွေက အသုံးဝင်ပြီး ရေးရတာ လွယ်ကူရိုးရှင်းပါတယ်။
SCSS
01.
@use "sass:math";
02.
03.
.container {
04.
display: flex;
05.
}
06.
07.
article[role="main"] {
08.
width: math.div(600px, 960px) * 100%;
09.
}
10.
11.
aside[role="complementary"] {
12.
width: math.div(300px, 960px) * 100%;
13.
margin-left: auto;
14.
}
Sass
01.
@use "sass:math"
02.
03.
.container
04.
display: flex
05.
06.
article[role="main"]
07.
width: math.div(600px, 960px) * 100%
08.
09.
aside[role="complementary"]
10.
width: math.div(300px, 960px) * 100%
11.
margin-left: auto
CSS
01.
.container {
02.
display: flex;
03.
}
04.
article[role=main] {
05.
width: 62.5%;
06.
}
07.
aside[role=complementary] {
08.
width: 31.25%;
09.
margin-left: auto;
10.
}
ဒီနေ့တော့ ဒီလောက်နဲ့ပဲ အဆုံးသတ်လိုက်ပါတော့မယ်။ ဖတ်ကြည့်လို့ အကျိုးတစ်စုံတစ်ရာတစ်ခုခု ရသွားတယ်ဆိုရင်ပဲ ကျေနပ်ပါတယ်။ အားလုံးကို ကျေးဇူးတင်ပါတယ်။
Reference
https://sass-lang.com/guide/
https://www.w3schools.com/sass/sass_intro.php