โมดูลในตัวของ SASS

โมดูลในตัวของ SASS

บทความนี้อธิบายโมดูลในตัวของ SASS

เราครอบคลุมโมดูลในตัวของ SASS แบบเป็นขั้นตอน ตั้งแต่พื้นฐานจนถึงการใช้งานระดับสูง

YouTube Video

โมดูลในตัวของ SASS

SASS มีโมดูลในตัวหลากหลาย การใช้งานโมดูลเหล่านี้ทำให้การเขียนสไตล์ชีตมีประสิทธิภาพยิ่งขึ้น

โมดูลในตัวของ SASS คืออะไร?

โมดูลในตัวของ SASS คือโมดูลที่มีฟังก์ชันและมิกซ์อินที่นำกลับมาใช้ซ้ำได้ การใช้โมดูลเหล่านี้ช่วยให้การคำนวณที่ซับซ้อนและการสร้างสไตล์แบบกำหนดเองทำได้ง่ายขึ้น

โมดูลในตัวหลัก ๆ มีดังต่อไปนี้:

  • sass:color
  • sass:string
  • sass:math
  • sass:list
  • sass:map
  • sass:selector
  • sass:meta

แต่ละโมดูลมีคุณสมบัติที่ช่วยให้การทำงานเฉพาะด้านราบรื่นขึ้น

รายละเอียดและตัวอย่างของแต่ละโมดูล

โมดูล sass:color

โมดูล sass:color มีฟังก์ชันที่ช่วยให้การปรับแต่งสีทำได้ง่าย

ฟังก์ชันหลัก
  • mix(): ผสมสีสองสี
  • adjust(): ปรับแต่งโทนสี ความสว่าง ความอิ่มตัว และคุณสมบัติอื่นๆ พร้อมกัน
ตัวอย่างการใช้งาน
 1@use "sass:color";
 2
 3$primary-color: #3498db;
 4$secondary-color: #e74c3c;
 5
 6// Mix two colors with equal weight
 7$blended-color: color.mix($primary-color, $secondary-color, 50%);
 8
 9// Adjust hue by 45 degrees using color.adjust()
10$adjusted-color: color.adjust($primary-color, $hue: 45deg);
11
12div {
13  background-color: $blended-color; // Result of mixing two colors
14  border-color: $adjusted-color;    // Hue adjusted by 45 degrees
15}
  • โค้ดนี้สร้างสีใหม่หนึ่งสีจากการผสมสองสี และอีกสีหนึ่งจากการปรับฮิว นำสีที่ได้ไปใช้กับองค์ประกอบเป็นสีพื้นหลังและสีเส้นขอบ ตัวอย่างนี้ช่วยให้เข้าใจพื้นฐานของการปรับแต่งสี

โมดูล sass:string

โมดูล sass:string มีฟังก์ชันที่เป็นประโยชน์สำหรับการจัดการสตริง

ฟังก์ชันหลัก
  • quote(), unquote(): ใส่หรือเอาเครื่องหมายอัญประกาศออกจากสตริง
  • length(): คืนค่าความยาวของสตริง
  • to-upper-case(), to-lower-case(): แปลงสตริงให้เป็นตัวพิมพ์ใหญ่หรือตัวพิมพ์เล็ก
ตัวอย่างการใช้งาน
 1@use "sass:string";
 2
 3// base values
 4$base-url: "https://example.com";
 5$path: "/assets/style.css";
 6
 7// 1) Combine strings using interpolation and then quote the result
 8$full-quoted: string.quote("#{$base-url}#{$path}");
 9// Example result: "\"https://example.com/assets/style.css\""
10
11// 2) Remove quotes from a quoted string
12$full-unquoted: string.unquote($full-quoted);
13// Example result: https://example.com/assets/style.css
14
15// 3) Get the length of the unquoted string
16$url-length: string.length($full-unquoted);
17// Example output: number of characters in the URL
18
19// 4) Convert strings to upper/lower case and quote for safe CSS output
20$block-name: "main-header";
21// "MAIN-HEADER"
22$upper-quoted: string.quote(string.to-upper-case($block-name));
23// "main-footer"
24$lower-quoted: string.quote(string.to-lower-case("MAIN-FOOTER"));
25
26a::after {
27  /* Use quoted strings for content to ensure valid CSS */
28  content: $full-quoted; /* "https://example.com/assets/style.css" */
29}
30
31:root {
32  /* Insert numeric values with interpolation when needed */
33  --url-length: #{ $url-length }; /* Example: --url-length: 31; */
34}
35
36.header::before {
37  /* Output uppercase version */
38  content: $upper-quoted; /* "MAIN-HEADER" */
39}
40
41.footer::after {
42  /* Output lowercase version */
43  content: $lower-quoted; /* "main-footer" */
44}
  • การใช้ string.quote() และ string.unquote() ช่วยให้ควบคุมรูปแบบการแสดงสตริงใน CSS ที่คอมไพล์ออกมาได้อย่างแม่นยำ string.length() เป็นฟังก์ชันสำหรับคืนค่าความยาวของสตริง string.to-upper-case() / string.to-lower-case() มีประโยชน์สำหรับการสร้างชื่อคลาสและจัดรูปแบบชื่อแบบ BEM

โมดูล sass:math

โมดูล sass:math มีฟังก์ชันสำหรับการคำนวณทางคณิตศาสตร์

ฟังก์ชันหลัก
  • pow(): ยกกำลัง
  • sqrt(): รากที่สอง
  • abs(): ค่าสัมบูรณ์
  • round(), ceil(), floor(): ปัดเศษ ปัดขึ้น ปัดลง
ตัวอย่างการใช้งาน
 1@use "sass:math";
 2
 3// Using pow() to calculate exponential values
 4$base-size: math.pow(2, 3) * 10px; // 80px
 5
 6// Using sqrt() to compute a square root
 7$root-size: math.sqrt(144) * 1px; // 12px
 8
 9// Using abs() to ensure a positive value
10$offset: math.abs(-15px); // 15px
11
12// Using round(), ceil(), and floor() for different rounding methods
13$rounded: math.round(12.6px); // 13px
14$ceiled: math.ceil(12.1px); // 13px
15$floored: math.floor(12.9px); // 12px
16
17.container {
18  width: $base-size; // 80px
19  height: $root-size; // 12px
20  margin-left: $offset; // 15px
21}
22
23.values {
24  /* Demonstrating different rounding operations */
25  padding: $rounded;  // 13px
26  border-width: $ceiled; // 13px
27  margin-top: $floored; // 12px
28}
  • math.pow() และ math.sqrt() มีประโยชน์ในการคำนวณขนาด ในขณะที่ math.abs() และฟังก์ชันการปัดช่วยจัดการการปรับค่า เมื่อนำมารวมกันจะช่วยให้คำนวณสเกล UI ที่สม่ำเสมอได้ง่าย

โมดูล sass:list

โมดูล sass:list มีฟังก์ชันเฉพาะสำหรับการจัดการลิสต์

ฟังก์ชันหลัก
  • append(): เพิ่มองค์ประกอบ
  • join(): รวมลิสต์
  • nth(): คืนค่าสมาชิกตามตำแหน่งที่กำหนด
  • length(): คืนค่าความยาวของลิสต์
ตัวอย่างการใช้งาน
 1@use "sass:list";
 2
 3// Base list
 4$colors: ("red", "blue", "green");
 5
 6// Add an element to the end of the list
 7$colors-appended: list.append($colors, "yellow");
 8// ("red", "blue", "green", "yellow")
 9
10// Add an element to the beginning of the list using join()
11$colors-prepended: list.join(("black",), $colors);
12// ("black", "red", "blue", "green", "yellow")
13
14// Join two lists together
15$extra-colors: ("pink", "cyan");
16$merged-colors: list.join($colors-prepended, $extra-colors);
17// ("black", "red", "blue", "green", "yellow", "pink", "cyan")
18
19// Get list length
20$total-length: list.length($merged-colors);
21
22// Example usage in a loop: assign each color to a list item
23ul {
24  @for $i from 1 through $total-length {
25    li:nth-child(#{$i}) {
26      /* Get the color at index $i */
27      color: list.nth($merged-colors, $i);
28    }
29  }
30}
  • คุณสามารถเพิ่มองค์ประกอบไปยังส่วนท้ายของรายการด้วย append() และใช้ join() เพื่อรวมหลายรายการเข้าด้วยกันอย่างยืดหยุ่น หากคุณต้องการเพิ่มองค์ประกอบที่จุดเริ่มต้น คุณสามารถทำได้โดยการรวมรายการที่มีองค์ประกอบเดียวเข้าด้านหน้าด้วย join() โดยการผสมผสาน length() และ nth() จะทำให้การสร้างรูปแบบ UI ที่ต้องใช้งานรายการแบบไดนามิกนั้นง่ายขึ้น

โมดูล sass:map

โมดูล sass:map มีฟังก์ชันสำหรับการทำงานกับแม็ป (associative arrays)

ฟังก์ชันหลัก
  • get(): ดึงค่าของคีย์ที่กำหนด
  • set(): เพิ่มหรืออัปเดตคู่คีย์-ค่า
  • keys(): ดึงคีย์ทั้งหมด
ตัวอย่างการใช้งาน
 1@use "sass:map";
 2
 3// Base theme map
 4$theme-colors: (
 5  "primary": #3498db,
 6  "secondary": #e74c3c
 7);
 8
 9// Update or add a value using set()
10$updated-theme: map.set($theme-colors, "warning", #f1c40f);
11// Map now has "warning": #f1c40f added
12
13// Get a value from the map
14$primary-color: map.get($updated-theme, "primary");
15
16// Get all keys from the map
17$all-keys: map.keys($updated-theme);
18// Example: ("primary", "secondary", "warning")
19
20button {
21  /* Apply color retrieved from the theme map */
22  background-color: $primary-color;
23}
24
25.debug {
26  /* Print keys as content for demonstration */
27  content: "#{$all-keys}";
28}
  • การใช้ map.set() ช่วยให้อัปเดตแม็ปแบบไดนามิกได้ และเมื่อใช้ร่วมกับ map.get() คุณสามารถสร้างโครงสร้างธีมที่ยืดหยุ่นได้ ด้วย map.keys() คุณสามารถแสดงรายการคีย์การตั้งค่าทั้งหมด ซึ่งช่วยในการออกแบบสไตล์ที่ขยายต่อได้

โมดูล sass:selector

โมดูล sass:selector มีฟังก์ชันที่เป็นประโยชน์สำหรับการจัดการตัวเลือก (selector)

ฟังก์ชันหลัก
  • nest(): ซ้อนตัวเลือก
  • is-superselector(): ตรวจสอบความครอบคลุมของตัวเลือก
  • replace(): แทนที่ตัวเลือก
ตัวอย่างการใช้งาน
 1@use "sass:selector";
 2
 3// Nest selectors (combine parent and child)
 4$nested-selector: selector.nest(".parent", ".child");
 5// Result: ".parent .child"
 6
 7// Check if one selector is a superselector of another
 8$is-super: selector.is-superselector(".parent", $nested-selector);
 9// true because ".parent" matches all elements that
10// ".parent .child" can match as an ancestor
11
12// Replace part of a selector with another selector
13$replaced-selector: selector.replace(".parent .child", ".child", ".item");
14// Result: ".parent .item"
15
16// Use generated selectors in actual CSS output
17#{$nested-selector} {
18  /* Applies to .parent .child */
19  color: red;
20}
21
22@if $is-super {
23  .info::after {
24    /* Demonstrate boolean result */
25    content: "parent is a superselector";
26  }
27}
28
29#{$replaced-selector} {
30  /* Applies to .parent .item */
31  background: blue;
32}
  • ใช้ selector.nest() เพื่อประกอบตัวเลือกอย่างยืดหยุ่น และใช้ selector.is-superselector() เพื่อตรวจสอบความสัมพันธ์ระหว่างตัวเลือก ผสานกับ selector.replace() เพื่อจัดการลอจิกการสร้างตัวเลือกระดับสูงได้อย่างกระชับ

โมดูล sass:meta

โมดูล sass:meta มีความสามารถที่เป็นประโยชน์สำหรับเมตาโปรแกรมมิงใน SASS

ฟังก์ชันหลัก
  • variable-exists(): ตรวจสอบว่ามีตัวแปรอยู่หรือไม่
  • global-variable-exists(): ตรวจสอบว่ามีตัวแปรแบบโกลบอลอยู่หรือไม่
  • inspect(): แสดงค่าเพื่อการดีบัก
ตัวอย่างการใช้งาน
 1@use "sass:meta";
 2
 3// Define a global variable
 4$color: #3498db;
 5
 6// Check if a variable exists in the current scope
 7@if meta.variable-exists("color") {
 8  body {
 9    /* Apply style only if $color exists */
10    background-color: $color;
11  }
12}
13
14// Create a local variable inside a block
15.container {
16  $local-size: 20px;
17
18  @if meta.variable-exists("local-size") {
19    /* Demonstrates detection of local variable */
20    width: $local-size;
21  }
22}
23
24// Check if a global variable exists
25$result: meta.global-variable-exists("color"); // true
26
27.debug {
28  /* Use inspect() to output the inspected value as a string */
29  content: meta.inspect($result); // "true"
30}
  • meta.variable-exists() และ meta.global-variable-exists() ช่วยให้ตรวจสอบสถานะของตัวแปรตามแต่ละสโคปได้อย่างปลอดภัย meta.inspect() มีประโยชน์มากสำหรับการดีบัก และสามารถแสดงค่าเป็นสตริงได้

ตัวอย่างการใช้งานจริง

การผสานหลายโมดูลในตัวเข้าด้วยกันช่วยเพิ่มความสามารถในการแสดงออกของ SASS มากยิ่งขึ้น ตัวอย่างด้านล่างใช้โมดูล color, math และ list ร่วมกันเพื่อทำงานประมวลผลสีและการจัดการลิสต์แบบอัตโนมัติ

 1@use "sass:color";
 2@use "sass:math";
 3@use "sass:list";
 4
 5// Base color list
 6$base-colors: (#3498db, #e74c3c, #2ecc71);
 7$darkened-colors: (); // Empty list for processed colors
 8
 9// Loop through each base color and darken it by 10%
10@each $color in $base-colors {
11  $darkened-colors: list.append(
12    $darkened-colors,
13    // Darken color by decreasing lightness by 10%
14    color.adjust($color, $lightness: -10%)
15  );
16}
17
18div {
19  // Apply each processed color to a corresponding <div>
20  @for $i from 1 through list.length($darkened-colors) {
21    &:nth-child(#{$i}) {
22      // Set color by index
23      background-color: list.nth($darkened-colors, $i);
24    }
25  }
26}
  • ในโค้ดนี้ สีที่ผ่านการประมวลผลจะถูกเพิ่มต่อเนื่องด้วย list.append() และใช้ color.adjust() โดยตั้งค่า $lightness: -10% เพื่อทำให้สีเข้มขึ้น 10% ท้ายที่สุด ใช้ @for ร่วมกับ list.nth() เพื่อกำหนดสีพื้นหลังที่ต่างกันให้แต่ละ <div>

สรุป

โมดูลในตัวของ SASS ช่วยเพิ่มความยืดหยุ่นให้กับ CSS เป็นอย่างมาก เมื่อเข้าใจแต่ละโมดูลและใช้อย่างเหมาะสม คุณจะสามารถสร้างสไตล์ชีตที่มีประสิทธิภาพและดูแลง่ายมากขึ้น

คุณสามารถติดตามบทความข้างต้นโดยใช้ Visual Studio Code บนช่อง YouTube ของเรา กรุณาตรวจสอบช่อง YouTube ด้วย

YouTube Video