Source

Bootstrap

Hindura Bootstrap 4 hamwe nuburyo bushya bwubatswe muri Sass ihindagurika kumiterere yisi yose ukunda kubintu byoroshye nibihinduka.

Intangiriro

Muri Bootstrap 3, insanganyamatsiko ahanini yatwarwaga nimpinduka zirenga muri LESS, CSS yihariye, hamwe nurupapuro rwihariye rwimiterere twashizemo muri distdosiye zacu. Hamwe nimbaraga, umuntu yashoboraga guhindura rwose isura ya Bootstrap 3 idakora kuri dosiye yibanze. Bootstrap 4 itanga uburyo bumenyerewe, ariko butandukanye gato.

Noneho, insanganyamatsiko irangizwa nimpinduka za Sass, ikarita ya Sass, hamwe na CSS yihariye. Nta yindi nsanganyamatsiko yihariye; ahubwo, urashobora gukora insanganyamatsiko yubatswe kugirango wongere gradients, igicucu, nibindi byinshi.

Sass

Koresha isoko yacu Sass dosiye kugirango ukoreshe impinduka, ikarita, ivanga, nibindi byinshi.

Imiterere ya dosiye

Igihe cyose bishoboka, irinde guhindura dosiye yibanze ya Bootstrap. Kuri Sass, bivuze gukora urupapuro rwawe bwite rutumiza Bootstrap kugirango ubashe guhindura no kuyagura. Dufate ko ukoresha paketi umuyobozi nka npm, uzagira imiterere ya dosiye isa nkiyi:

your-project/
├── scss
│   └── custom.scss
└── node_modules/
    └── bootstrap
        ├── js
        └── scss

Niba warakuyeho amadosiye yinkomoko kandi ukaba udakoresha pake yumuyobozi, uzakenera gushiraho intoki ikintu gisa nkiyo miterere, ukagumana dosiye yinkomoko ya Bootstrap itandukanye niyanyu.

your-project/
├── scss
│   └── custom.scss
└── bootstrap/
    ├── js
    └── scss

Kuzana ibicuruzwa

Muriwe custom.scss, uzatumiza inkomoko ya Bootstrap ya Sass dosiye. Ufite amahitamo abiri: shyiramo Bootstrap yose, cyangwa hitamo ibice ukeneye. Turashishikariza aba nyuma, nubwo menya ko hari ibyo dusabwa hamwe nubwishingizi mubice byacu. Uzakenera kandi gushyiramo JavaScript ya plugins zacu.

// Custom.scss
// Option A: Include all of Bootstrap

@import "../node_modules/bootstrap/scss/bootstrap";
// Custom.scss
// Option B: Include parts of Bootstrap

// Required
@import "../node_modules/bootstrap/scss/functions";
@import "../node_modules/bootstrap/scss/variables";
@import "../node_modules/bootstrap/scss/mixins";

// Optional
@import "../node_modules/bootstrap/scss/reboot";
@import "../node_modules/bootstrap/scss/type";
@import "../node_modules/bootstrap/scss/images";
@import "../node_modules/bootstrap/scss/code";
@import "../node_modules/bootstrap/scss/grid";

Hamwe nuburyo bwashyizweho, urashobora gutangira guhindura icyaricyo cyose cya Sass hamwe namakarita yawe custom.scss. Urashobora kandi gutangira kongeramo ibice bya Bootstrap munsi yicyiciro // Optionalnkuko bikenewe. Turasaba gukoresha ibicuruzwa byuzuye biva muri bootstrap.scssdosiye yacu aho utangirira.

Impinduka zisanzwe

Buri Sass ihinduka muri Bootstrap 4 ikubiyemo !defaultibendera ryemerera kurenga agaciro gasanzwe kahinduwe muri Sass yawe bwite udahinduye code ya Bootstrap. Gukoporora no gukata impinduka nkuko bikenewe, uhindure indangagaciro, kandi ukureho !defaultibendera. Niba impinduka zimaze gutangwa, ntabwo izongera kugenwa nagaciro gasanzwe muri Bootstrap.

Uzasangamo urutonde rwuzuye rwa Bootstrap ihinduka muri scss/_variables.scss.

Ibihinduka birenze muri dosiye imwe ya Sass irashobora kuza mbere cyangwa nyuma yimiterere ihindagurika. Ariko, mugihe urenze hejuru ya dosiye ya Sass, ibirenga byawe bigomba kuza mbere yuko winjiza dosiye ya Sass ya Bootstrap.

Dore urugero ruhindura i background-colorna colorkuri <body>iyo mugihe cyo gutumiza no gukusanya Bootstrap ukoresheje npm:

// Your variable overrides
$body-bg: #000;
$body-color: #111;

// Bootstrap and its default variables
@import "../node_modules/bootstrap/scss/bootstrap";

Subiramo nkibikenewe kubihinduka byose muri Bootstrap, harimo amahitamo yisi yose hepfo.

Ikarita

Bootstrap 4 ikubiyemo amakarita ya Sass yintoki, ibyingenzi byingenzi byoroha kubyara imiryango ya CSS ifitanye isano. Dukoresha amakarita ya Sass kumabara yacu, grid yacitse, nibindi byinshi. Nka Sass ihinduka, amakarita yose ya Sass arimo !defaultibendera kandi arashobora kurengerwa no kwagurwa.

Amwe mu makarita yacu ya Sass yahujwe nubusa kubusa. Ibi bikorwa kugirango yemere kwaguka byoroshye ikarita ya Sass yatanzwe, ariko iza ku kiguzi cyo gukora ibintu ku ikarita bigoye cyane.

Hindura ikarita

Guhindura ibara risanzwe kurikarita yacu $theme-colors, ongeraho ibikurikira muri dosiye yawe ya Sass:

$theme-colors: (
  "primary": #0074d9,
  "danger": #ff4136
);

Ongera ku ikarita

Kongera ibara rishya kuri $theme-colors, ongeramo urufunguzo rushya nagaciro:

$theme-colors: (
  "custom-color": #900
);

Kura ku ikarita

Kuraho amabara $theme-colors, cyangwa indi karita iyo ari yo yose, koresha map-remove. Menya neza ko ugomba kubishyira hagati y'ibyo dusabwa n'amahitamo:

// Required
@import "../node_modules/bootstrap/scss/functions";
@import "../node_modules/bootstrap/scss/variables";
@import "../node_modules/bootstrap/scss/mixins";

$theme-colors: map-remove($theme-colors, "info", "light", "dark");

// Optional
@import "../node_modules/bootstrap/scss/root";
@import "../node_modules/bootstrap/scss/reboot";
@import "../node_modules/bootstrap/scss/type";
...

Urufunguzo rusabwa

Bootstrap ifata ko hari urufunguzo rwihariye mu ikarita ya Sass nkuko twakoresheje kandi twagura ubwacu. Mugihe uhinduye amakarita arimo, urashobora guhura namakosa aho urufunguzo rwikarita ya Sass rukoreshwa.

Kurugero, dukoresha i primary,, successna dangerurufunguzo kuva kumurongo $theme-colors, buto, na form leta. Gusimbuza indangagaciro zuru rufunguzo ntibigomba kwerekana ibibazo, ariko kubikuraho bishobora gutera ibibazo byo gukusanya Sass. Muri ibi bihe, uzakenera guhindura code ya Sass ikoresha izo ndangagaciro.

Imikorere

Bootstrap ikoresha ibikorwa byinshi bya Sass, ariko igice kimwe gusa kirakoreshwa kumutwe rusange. Twashizemo imikorere itatu yo kubona indangagaciro kuva ku ikarita y'amabara:

@function color($key: "blue") {
  @return map-get($colors, $key);
}

@function theme-color($key: "primary") {
  @return map-get($theme-colors, $key);
}

@function gray($key: "100") {
  @return map-get($grays, $key);
}

Ibi biragufasha guhitamo ibara rimwe kurikarita ya Sass nkukuntu wakoresha ibara rihinduka kuva v3.

.custom-element {
  color: gray("100");
  background-color: theme-color("dark");
}

Dufite kandi ikindi gikorwa cyo kubona urwego runaka rwamabara kurikarita $theme-colors. Urwego rubi indangagaciro zizorohereza ibara, mugihe urwego rwo hejuru ruzijimye.

@function theme-color-level($color-name: "primary", $level: 0) {
  $color: theme-color($color-name);
  $color-base: if($level > 0, #000, #fff);
  $level: abs($level);

  @return mix($color-base, $color, $level * $theme-color-interval);
}

Mu myitozo, wahamagara imikorere hanyuma ukanyuza mubice bibiri: izina ryibara kuva $theme-colors(urugero, ibanze cyangwa akaga) nurwego rwumubare.

.custom-element {
  color: theme-color-level(primary, -10);
}

Ibikorwa byinyongera birashobora kongerwaho mugihe kizaza cyangwa kugiti cyawe Sass kugirango ukore ibikorwa byurwego rwikarita yinyongera ya Sass, cyangwa nibisanzwe niba ushaka kuba inshinga nyinshi.

Itandukaniro ryamabara

Igikorwa kimwe cyinyongera dushyira muri Bootstrap nigikorwa cyo gutandukanya ibara , color-yiq. Ikoresha ibara ryamabara YIQ kugirango ihite isubiza urumuri ( #fff) cyangwa umwijima ( #111) rutandukanye rushingiye kumabara yibanze. Iyi mikorere ni ingirakamaro cyane cyane kuvanga cyangwa kuzenguruka aho ubyara ibyiciro byinshi.

Kurugero, kubyara amabara yaturutse ku $theme-colorsikarita yacu:

@each $color, $value in $theme-colors {
  .swatch-#{$color} {
    color: color-yiq($value);
  }
}

Irashobora kandi gukoreshwa muburyo bumwe butandukanye:

.custom-element {
  color: color-yiq(#000); // returns `color: #fff`
}

Urashobora kandi kwerekana ibara shingiro hamwe nibikorwa byikarita yamabara:

.custom-element {
  color: color-yiq(theme-color("dark")); // returns `color: #fff`
}

Amahitamo ya Sass

Hindura Bootstrap 4 hamwe na fayili yacu yubatswe yihariye kandi uhindure byoroshye CSS yisi yose hamwe nibintu bishya bya $enable-*Sass. Kurenga agaciro k'ibihinduka hanyuma usubiremo npm run testnkuko bikenewe.

Urashobora kubona no guhitamo ibyo bihinduka kubintu byingenzi byisi yose muri scss/_variables.scssdosiye ya Bootstrap.

Birahinduka Indangagaciro Ibisobanuro
$spacer 1rem(Mburabuzi), cyangwa agaciro ako ari ko kose> 0 Kugaragaza agaciro gasanzwe kamaro kugirango gahunda itange umusaruro utanga ibikoresho .
$enable-rounded true(Mburabuzi) cyangwafalse Gushoboza uburyo bwateganijwe mbere border-radiuskubintu bitandukanye.
$enable-shadows truecyangwa false(Mburabuzi) Gushoboza uburyo bwateganijwe mbere box-shadowkubintu bitandukanye.
$enable-gradients truecyangwa false(Mburabuzi) Gushoboza gradients zateganijwe binyuze background-imagemuburyo butandukanye.
$enable-transitions true(Mburabuzi) cyangwafalse Gushoboza transitions byateganijwe mbere kubice bitandukanye.
$enable-hover-media-query truecyangwa false(Mburabuzi) Gutesha agaciro
$enable-grid-classes true(Mburabuzi) cyangwafalse Gushoboza ibisekuru bya CSS ya sisitemu ya gride (urugero ,,,, .containernibindi .row) .col-md-1.
$enable-caret true(Mburabuzi) cyangwafalse Gushoboza pseudo element caret kuri .dropdown-toggle.
$enable-print-styles true(Mburabuzi) cyangwafalse Gushoboza uburyo bwo guhitamo icapiro.

Ibara

Byinshi mubintu bitandukanye bya Bootstrap nibikorwa byubatswe binyuze murukurikirane rwamabara asobanurwa mukarita ya Sass. Ikarita irashobora kuzunguruka muri Sass kugirango byihute kubyara urukurikirane rw'amategeko.

Amabara yose

Amabara yose aboneka muri Bootstrap 4, arahari nkibihinduka bya Sass n'ikarita ya Sass muri scss/_variables.scssdosiye. Ibi bizagurwa mugihe gito gisohotse kugirango hongerwemo igicucu, cyane nka palette palette dusanzwe dushyiramo.

Ubururu
Indigo
Umutuku
Umutuku
Umutuku
Icunga
Umuhondo
Icyatsi
Icyayi
Cyan

Dore uko ushobora gukoresha ibi muri Sass yawe:

// With variable
.alpha { color: $purple; }

// From the Sass map with our `color()` function
.beta { color: color("purple"); }

Amabara yingirakamaro yamasomo nayo arahari mugushiraho colorna background-color.

Mugihe kizaza, tuzagerageza gutanga amakarita ya Sass nibihinduka kubicucu bya buri bara nkuko twabikoze hamwe namabara ya graycale hepfo.

Amabara yibanze

Dukoresha igice cyamabara yose kugirango dukore palette ntoya yo kubyara amabara, nayo iraboneka nkibihinduka bya Sass hamwe nikarita ya Sass muri scss/_variables.scssdosiye ya Bootstraps.

Ibanze
Secondary
Intsinzi
Akaga
Iburira
Amakuru
Umucyo
Umwijima

Icyatsi

Ikirangantego cyagutse cyimyenda ihindagurika hamwe nikarita ya Sass muburyo scss/_variables.scssbwigicucu kijimye cyumusatsi kumushinga wawe.

100
200
300
400
500
600
700
800
900

Imbere scss/_variables.scss, uzasangamo ibara rya Bootstrap hamwe namakarita ya Sass. Dore urugero $colorsrw'ikarita ya Sass:

$colors: (
  "blue": $blue,
  "indigo": $indigo,
  "purple": $purple,
  "pink": $pink,
  "red": $red,
  "orange": $orange,
  "yellow": $yellow,
  "green": $green,
  "teal": $teal,
  "cyan": $cyan,
  "white": $white,
  "gray": $gray-600,
  "gray-dark": $gray-800
) !default;

Ongeraho, ukureho, cyangwa uhindure indangagaciro mukarita kugirango uvugurure uko zikoreshwa mubindi bice byinshi. Kubwamahirwe muriki gihe, ntabwo buri kintu cyose gikoresha ikarita ya Sass. Ibihe bizaza bizaharanira kunoza ibi. Kugeza icyo gihe, teganya gukoresha ${color}impinduka niyi karita ya Sass.

Ibigize

Byinshi mubice bya Bootstrap nibikoresho byingirakamaro byubatswe hamwe @eachnibisubirwamo hejuru yikarita ya Sass. Ibi bifasha cyane cyane kubyara ibice bigize ibice byacu $theme-colorsno gukora impinduka zishubije kuri buri gice. Mugihe uhinduye amakarita ya Sass hanyuma ukayasubiramo, uzahita ubona impinduka zawe zigaragara muriyi mirongo.

Abahindura

Byinshi mubigize Bootstrap byubatswe hamwe na base-modifier class approach. Ibi bivuze ko igice kinini cyimyandikire gikubiye mubyiciro fatizo (urugero, .btn) mugihe imiterere itandukanye igarukira kumasomo yo guhindura (urugero, .btn-danger). Aya masomo yo guhindura yubatswe kuva ku $theme-colorsikarita kugirango uhindure umubare nizina ryibyiciro byacu byo guhindura.

Hano hari ingero ebyiri zuburyo twazengurutse $theme-colorsikarita kugirango tubyare abahindura .alertibice hamwe nibikorwa byacu byose byingenzi .bg-*.

// Generate alert modifier classes
@each $color, $value in $theme-colors {
  .alert-#{$color} {
    @include alert-variant(theme-color-level($color, -10), theme-color-level($color, -9), theme-color-level($color, 6));
  }
}

// Generate `.bg-*` color utilities
@each $color, $value in $theme-colors {
  @include bg-variant('.bg-#{$color}', $value);
}

Igisubizo

Utuzingo twa Sass ntabwo tugarukira ku ikarita y'amabara, haba. Urashobora kandi kubyara itandukaniro ryibisubizo byibigize cyangwa ibikorwa byawe. Fata nkurugero rwibisubizo byingirakamaro byingirakamaro aho tuvanga ikarita yikarita @eachya $grid-breakpointsSass hamwe nibibazo byitangazamakuru birimo.

@each $breakpoint in map-keys($grid-breakpoints) {
  @include media-breakpoint-up($breakpoint) {
    $infix: breakpoint-infix($breakpoint, $grid-breakpoints);

    .text#{$infix}-left   { text-align: left !important; }
    .text#{$infix}-right  { text-align: right !important; }
    .text#{$infix}-center { text-align: center !important; }
  }
}

Ukeneye guhindura ibyawe $grid-breakpoints, impinduka zawe zizakoreshwa kumuzingo wose uzenguruka kuri iyo karita.

Impinduka za CSS

Bootstrap 4 ikubiyemo ibintu bibiri bya CSS byihariye (variable) muri CSS yakozwe. Ibi bitanga uburyo bworoshye bwo kubona indangagaciro zikoreshwa nkamabara yinsanganyamatsiko, gucamo, hamwe nimyandikire yibanze mugihe ukora muri Inspecteur ya mushakisha yawe, agasanduku kode, cyangwa prototyping rusange.

Ibihinduka biboneka

Dore impinduka dushyiramo (menya ko :rootibisabwa). Biri muri _root.scssdosiye yacu.

:root {
  --blue: #007bff;
  --indigo: #6610f2;
  --purple: #6f42c1;
  --pink: #e83e8c;
  --red: #dc3545;
  --orange: #fd7e14;
  --yellow: #ffc107;
  --green: #28a745;
  --teal: #20c997;
  --cyan: #17a2b8;
  --white: #fff;
  --gray: #6c757d;
  --gray-dark: #343a40;
  --primary: #007bff;
  --secondary: #6c757d;
  --success: #28a745;
  --info: #17a2b8;
  --warning: #ffc107;
  --danger: #dc3545;
  --light: #f8f9fa;
  --dark: #343a40;
  --breakpoint-xs: 0;
  --breakpoint-sm: 576px;
  --breakpoint-md: 768px;
  --breakpoint-lg: 992px;
  --breakpoint-xl: 1200px;
  --font-family-sans-serif: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
  --font-family-monospace: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
}

Ingero

Impinduka za CSS zitanga ihinduka risa nimpinduka za Sass, ariko bidakenewe gukusanya mbere yo guhabwa mushakisha. Kurugero, hano turimo gusubiramo imyandikire yurupapuro rwacu hamwe nuburyo bwo guhuza hamwe na CSS ihinduka.

body {
  font: 1rem/1.5 var(--font-family-sans-serif);
}
a {
  color: var(--blue);
}

Impinduka zinyuranye

Mugihe twabanje gushyiramo ibice byahinduwe muri CSS yacu (urugero, --breakpoint-md), ibyo ntibishyigikiwe mubibazo byitangazamakuru , ariko birashobora gukoreshwa mumategeko mubibazo byitangazamakuru. Izi mpinduka zinyuranye ziguma muri CSS yakozwe kugirango ihuze inyuma bitewe nuko ishobora gukoreshwa na JavaScript. Wige byinshi muri sp.

Dore urugero rwibidashyigikiwe:

@media (min-width: var(--breakpoint-sm)) {
  ...
}

Kandi dore urugero rwibyashyigikiwe:

@media (min-width: 768px) {
  .custom-element {
    color: var(--primary);
  }
}