Direktif @at-root di SASS

Direktif @at-root ditujukan untuk mengekstrak aturan dari selektor induk ke root file.

Mari kita lihat contoh berikut dengan selektor tingkat bersarang pertama:

style.scss.parent {
  background-color: #fdd;
  @at-root .child {
    border: 1px solid;
  }
}

Hasil kompilasinya adalah kode berikut:

styles.css.parent {
  background-color: #fdd;
}
.child {
  border: 1px solid;
}

Sekarang mari kita lihat cara kerja direktif @at-root dengan beberapa selektor:

style.scss.main-parent {
  background-color: #fdd;
  @at-root {
    .first-child {
      border: 1px solid;
    }
    .second-child {
      font-weight: bold;
    }  
  }
  .step-child {
    color: #232523;
  }
}

Setelah kompilasi, kodenya terlihat seperti ini:

styles.css.main-parent {
  background-color: #fdd; 
}

.first-child {
  border: 1px solid; 
}

.second-child {
  font-weight: bold; 
}

.main-parent .step-child {
  color: #232523; 
}
Задание 1

Jelaskan, apa hasil kompilasi dari kode berikut:

styles.scss.primary-button {
  background-color: #3272c7;
  @at-root {
    .block-button {
      color: #ff0000;
    }
    .content-button {
      color: #ccb42a;
    }
  }
  .warning-button {
    border: 2px solid #232523;
  }
}