@charset "UTF-8";
@import "./variables.scss";

//-----------------------------------------------------
// mixin scss
// mixin，通过@include调用，样式通过拷贝的方式使用，尤其适用于传递参数
// %，通过@extend调用，样式通过组合申明的方式使用，适用于不传参数的代码片段
// mixin & % 既定义了mixin也定义了%，根据需求使用@include或@extend调用
//-----------------------------------------------------

// Center-align a block level element
@mixin center-block($extend: true) {
  @if $extend {
    @extend %center-block;
  } @else {
    margin-left: auto;
    margin-right: auto;
  }
}

%center-block {
  @include center-block(false);
}

// clear
@mixin clear($extend: true) {
  @if $extend {
    @extend %clear;
  } @else {
    &:after {
      content: ".";
      display: block;
      height: 0;
      clear: both;
      visibility: hidden;
    }
  }
}

%clear {
  @include clear(false);
}

// Hide only visually, but have it available for screenReaders
// 只隐藏于视觉，屏幕浏览器可以阅读
@mixin hidden-clip($extend: true) {
  @if $extend {
    @extend %hidden-clip;
  } @else {
    position: absolute;
    clip: rect(1px, 1px, 1px, 1px);
  }
}

%hidden-clip {
  @include hidden-clip(false);
}

// ellipsis
@mixin ellipsis($extend: true) {
  @if $extend {
    @extend %ellipsis;
  } @else {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
}

%ellipsis {
  @include ellipsis(false);
}

// ellipsis lines
// only old webkit flex box
@mixin ellipsis-lines($lines: 2) {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: $lines;
  -webkit-box-orient: vertical;
}

// word-break
@mixin word-break($extend: true) {
  @if $extend {
    @extend %word-break;
  } @else {
    white-space: normal;
    word-wrap: break-word;
    word-break: break-all;
  }
}

%word-break {
  @include word-break(false);
}

// disabled
@mixin disabled($colorText: $disable-text-color, $colorBg: $disable-background-color, $colorBorder: $disable-background-color) {
  background-color: $colorBg !important;
  color: $colorText !important;
  cursor: default !important;
  pointer-events: none !important;
  @if $colorBorder {
    border: 1px solid $colorBorder;
  }
}

%disabled {
  @include disabled;
}

// image replace text
@mixin ir($extend: true) {
  @if $extend {
    @extend %ir;
  } @else {
    font: 0/0 a;
    text-shadow: none;
    border: 0 none;
    color: transparent;
  }
}

%ir {
  @include ir(false);
}

// fixed top or bottom or bottom & top
@mixin fixed($pos: 0) {
  position: fixed;
  left: 0;
  right: 0;
  @if $pos == bottom {
    bottom: 0;
  } @else {
    @if $pos == all {
      top: 0;
      bottom: 0;
    } @else {
      top: $pos;
    }
  }
}

%fixed-top {
  @include fixed;
}

%fixed-bottom {
  @include fixed(bottom);
}

// justify
// 左右对齐
@mixin justify($extend: true) {
  @if $extend {
    @extend %justify;
  } @else {
    display: flex;
    justify-content: space-between;
  }
}

%justify {
  @include justify(false);
}

// retina border
// 0.5px实现 ios9
@mixin retina-one-px() {
  @supports (border-width: 0.5px) {
    @media only screen and (-webkit-min-device-pixel-ratio: 2), screen and (-webkit-min-device-pixel-ratio: 3) {
      border-width: 0.5px;
    }
  }
}

// linear-gradient实现
// 安卓4.3- 不支持background-size的百分比
@mixin retina-one-px-bg($direction: top, $color: $border-color) {
  background-repeat: no-repeat;
  @if $direction == top {
    background-image: linear-gradient(to bottom, $color 50%, transparent 50%);
    background-size: 100% 1px;
  }
  @if $direction == bottom {
    background-image: linear-gradient(to top, $color 50%, transparent 50%);
    background-size: 100% 1px;
    background-position: left bottom;
  }
  @if $direction == left {
    background-image: linear-gradient(to right, $color 50%, transparent 50%);
    background-size: 1px 100%;
  }
  @if $direction == right {
    background-image: linear-gradient(to left, $color 50%, transparent 50%);
    background-size: 1px 100%;
    background-position: right top;
  }
  @if $direction == v { // 左右两个边框
    background-image: linear-gradient(to right, $color 50%, transparent 50%), linear-gradient(to left, $color 50%, transparent 50%);
    background-size: 1px 100%;
    background-position: left top, right top;
  }
  @if $direction == h { // 上下两个边框
    background-image: linear-gradient(to bottom, $color 50%, transparent 50%), linear-gradient(to top, $color 50%, transparent 50%);
    background-size: 100% 1px;
    background-position: left top, left bottom;
  }
  @if $direction == all { // 上下左右四个边框
    background-image: linear-gradient(to bottom, $color 50%, transparent 50%), linear-gradient(to top, $color 50%, transparent 50%), linear-gradient(to right, $color 50%, transparent 50%), linear-gradient(to left, $color 50%, transparent 50%);
    background-size: 100% 1px, 100% 1px, 1px 100%, 1px 100%;
    background-position: left top, left bottom, left top, right top;
  }
}

// border和transform实现
// 注意before和after的层级问题
@mixin retina-one-px-border($direction: top, $color: $border-color) {
  position: absolute;
  left: 0;
  top: 0;
  box-sizing: border-box;

  @if $direction == top or $direction == bottom {
    right: 0;
    height: 0;
    transform: scaleY(0.5);
    border-top: 1px solid $color;
  }
  @if $direction == bottom {
    top: auto;
    bottom: 0;
  }
  @if $direction == right or $direction == left {
    width: 0;
    bottom: 0;
    transform: scaleX(0.5);
    border-left: 1px solid $color;
  }
  @if $direction == right {
    left: auto;
    right: 0;
  }
  @if $direction == all {
    width: 200%;
    height: 200%;
    transform-origin: left top;
    transform: scale(0.5);
    border: 1px solid $color;
  }
}

// border top & bottom
%border-tb {
  position: relative;

  &::before {
    content: "";
    @include retina-one-px-border(top);
    z-index: 1;
  }

  &::after {
    content: "";
    @include retina-one-px-border(bottom);
  }
}

// border all
%border-all {
  position: relative;

  &::before {
    content: "";
    @include retina-one-px-border(all);
    z-index: -1;
  }
}

// mixin
// 只定义了mixin，所以只能通过@include来调用
//-----------------------------------------------------

// table 等
// $child 参数请使用单引号，因为用于子元素选择器
@mixin equal-table($child: li) {
  display: table;
  table-layout: fixed;
  width: 100%;

  #{$child} {
    display: table-cell;
  }
}

// flex 等分
// $child 参数请使用单引号，因为用于子元素选择器
@mixin equal-flex($child: li) {
  display: flex;

  #{$child} {
    flex: 1;
    width: 1%;
  }
}

// line equal gap
// $child 参数请使用单引号，因为用于子元素选择器
@mixin line-equal-gap($gap: 10px, $child: li, $lr: true) {
  display: flex;

  @if $lr {
    padding-left: $gap;
    padding-right: $gap;
  }

  #{$child} {
    flex: 1;
    width: 1%;

    &:not(:first-of-type) {
      margin-left: $gap;
    }
  }
}

// line equal item
@mixin line-equal-item($lr: true) {
  display: flex;
  justify-content: space-between;
  @if $lr {
    &::before,
    &::after {
      content: "";
    }
  }
}

// flex center
@mixin center-flex($direction: both) {
  display: flex;
  @if $direction == both {
    justify-content: center;
    align-items: center;
  }
  @if $direction == x {
    justify-content: center;
  }
  @if $direction == y {
    align-items: center;
  }
}

// translate center
@mixin center-translate($direction: both) {
  position: absolute;
  @if $direction == both {
    top: 50%;
    left: 50%;
    transform: translate3d(-50%, -50%, 0);
  }
  @if $direction == x {
    left: 50%;
    transform: translate3d(-50%, 0, 0);
  }
  @if $direction == y {
    top: 50%;
    transform: translate3d(0, -50%, 0);
  }
}

// object wrap
// $child 参数请使用单引号，因为用于子元素选择器
@mixin object-wrap($percent: 100%, $child: img) {
  position: relative;
  padding-top: $percent;
  height: 0;

  #{$child} {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
  }
}

// triangle 三角箭头
// 可采用空元素或伪元素生成，具体定位这里不涉及
%triangle-basic {
  content: "";
  height: 0;
  width: 0;
  overflow: hidden;
}

@mixin triangle($direction: top, $borderWidth: 6px, $borderColor: $border-color) {
  @extend %triangle-basic;
  @if $direction == top {
    border-bottom: $borderWidth solid $borderColor;
    border-left: $borderWidth dashed transparent;
    border-right: $borderWidth dashed transparent;
  }
  @if $direction == right {
    border-left: $borderWidth solid $borderColor;
    border-top: $borderWidth dashed transparent;
    border-bottom: $borderWidth dashed transparent;
  }
  @if $direction == bottom {
    border-top: $borderWidth solid $borderColor;
    border-left: $borderWidth dashed transparent;
    border-right: $borderWidth dashed transparent;
  }
  @if $direction == left {
    border-right: $borderWidth solid $borderColor;
    border-top: $borderWidth dashed transparent;
    border-bottom: $borderWidth dashed transparent;
  }
}

// v arrow 方向箭头
@mixin v-arrow($direction: right, $borderWidth: 2px, $size: 10px) {
  display: inline-block;
  vertical-align: middle;
  width: $size;
  height: $size;
  @if $direction == top {
    border-top: $borderWidth solid $placeholder-color;
    border-right: $borderWidth solid $placeholder-color;
    transform: rotate(-45deg);
  }
  @if $direction == right {
    border-top: $borderWidth solid $placeholder-color;
    border-right: $borderWidth solid $placeholder-color;
    transform: rotate(45deg);
  }
  @if $direction == bottom {
    border-left: $borderWidth solid $placeholder-color;
    border-bottom: $borderWidth solid $placeholder-color;
    transform: rotate(-45deg);
  }
  @if $direction == left {
    border-left: $borderWidth solid $placeholder-color;
    border-bottom: $borderWidth solid $placeholder-color;
    transform: rotate(45deg);
  }
}

// animation-fade
// @include animation-fade
// @include animation-fade($from: false, $to: .5);
// @include animation-fade($from: 1, $to: 0);
@mixin animation-fade($name: animationFade, $from: 0, $to: false) {
  @keyframes #{$name} {
    @if $from {
      from {
        opacity: $from;
      }
    }
    @if $to {
      to {
        opacity: $to;
      }
    }
  }
}

// %
// 只定义了%，所以只能通过@extend来调用
//-----------------------------------------------------

// bar line
%bar-line {
  line-height: $line-height - 10px;
  padding: 5px 10px;
  position: relative;
  display: block;
  overflow: hidden;
  @if $active-state-switch {
    &:active,
    &:hover {
      background-color: darken($background-color, 3%);
    }
  }

  &:not(:first-of-type)::before {
    content: "";
    @include retina-one-px-border;
  }
}

// item arrow, 右侧箭头跳转指向
%item-v-right {
  &::after {
    content: "";
    @include v-arrow;
    color: $placeholder-color;
    position: absolute;
    right: 15px;
    top: 50%;
    margin-top: -1px;
    transform: rotate(45deg) translate(0, -50%);
    box-sizing: border-box;
  }
}

// 间隔列表
%gap-item {
  position: relative;
  background: #fff;
  margin: 10px 0;
  @if $active-state-switch {
    &:active,
    &:hover {
      background-color: darken($background-color, 3%);
    }
  }

  &::before {
    content: "";
    @include retina-one-px-border;
  }

  &::after {
    content: "";
    @include retina-one-px-border(bottom);
  }
}

// fullscreen center
%fullscreen-center {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  margin: auto;
}

// 下面的几个%，由于版本或前缀，所以设计成%
//-----------------------------------------------------
// flex
%display-flex {
  display: flex;
}

// all-transition
%transition-all {
  transition: all 0.3s ease-in-out;
}

// translate3d
%translate3d {
  transform: translate3d(0, 0, 0);
}


// btn
//----------------------------------------------------
// btn-basic
// 按钮基本样式，联合申明
%btn-basic {
  display: inline-block;
  vertical-align: middle;
  cursor: pointer;
  text-align: center;
  border: 1px solid transparent;
  box-sizing: border-box;
  user-select: none;
  padding: 0 1em;
  white-space: nowrap;
}

// btn-size
// 按钮大小
@mixin btn-size($padding: 1em, $height: $line-height, $radius: 3px) {
  padding: 0 $padding;
  line-height: $height - 2px; // 减掉2px的上下高度
  @if $radius {
    border-radius: $radius;
  }
}

// btn-color
// 包括按钮背景色，文本色，是否有边框
@mixin btn-color($colorText: #333, $colorBg: #666, $colorBorder: false) {
  color: $colorText;
  background-color: $colorBg;
  @if $colorBorder {
    border-color: $colorBorder;
  }
}
