SASS-এর বিল্ট-ইন মডিউলসমূহ
এই নিবন্ধে SASS-এর বিল্ট-ইন মডিউলসমূহ ব্যাখ্যা করা হয়েছে।
আমরা বেসিক থেকে উন্নত ব্যবহারের দিক পর্যন্ত ধাপে ধাপে SASS-এর বিল্ট-ইন মডিউলসমূহ কভার করছি।
YouTube Video
SASS-এর বিল্ট-ইন মডিউলসমূহ
SASS নানান ধরনের বিল্ট-ইন মডিউল সরবরাহ করে, এবং এগুলো ব্যবহার করলে স্টাইলশীট লেখা আরও দক্ষ হয়।
SASS-এর বিল্ট-ইন মডিউলগুলো কী?
SASS-এর বিল্ট-ইন মডিউল হলো পুনর্ব্যবহারযোগ্য ফাংশন ও মিক্সিন সরবরাহকারী মডিউলসমূহ। এগুলো ব্যবহার করলে জটিল গণনা ও কাস্টম স্টাইল তৈরি করা সহজ হয়।
প্রধান বিল্ট-ইন মডিউলগুলোর মধ্যে রয়েছে:।
sass:colorsass:stringsass:mathsass:listsass:mapsass:selectorsass: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 মডিউল ম্যাপ (অ্যাসোসিয়েটিভ অ্যারে) নিয়ে কাজ করার ফাংশন সরবরাহ করে।
প্রধান ফাংশনসমূহ
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 মডিউল সিলেক্টর ম্যানিপুলেশনে সহায়ক ফাংশন সরবরাহ করে।
প্রধান ফাংশনসমূহ
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%ব্যবহার করে রংগুলোকে ১০% গা dark য় করা হয়েছে। শেষে, প্রতিটি<div>-এ ভিন্ন ভিন্ন ব্যাকগ্রাউন্ড রঙ প্রয়োগ করতে@for-এর সাথেlist.nth()মিলিয়ে ব্যবহার করা হয়েছে।
সারসংক্ষেপ
SASS-এর বিল্ট-ইন মডিউলসমূহ CSS-এর নমনীয়তা উল্লেখযোগ্যভাবে বাড়ায়। প্রতিটি মডিউল বুঝে উপযুক্তভাবে ব্যবহার করলে আরও দক্ষ ও রক্ষণাবেক্ষণযোগ্য স্টাইলশীট তৈরি করা যায়।
আপনি আমাদের ইউটিউব চ্যানেলে ভিজ্যুয়াল স্টুডিও কোড ব্যবহার করে উপরের নিবন্ধটি অনুসরণ করতে পারেন। দয়া করে ইউটিউব চ্যানেলটিও দেখুন।