<template>
  <div class="zui-code-snippet">
    <div class="zui-code-snippet--demo">
      <slot name="source"></slot>
    </div>
    <div class="zui-code-snippet--desc">
      <slot name="desc"></slot>
      <span class="zui-code-snippet--icon-code" @click="showCode = !showCode" title="查看代码">
        <img v-if="showCode" src="@/assets/images/icon/code-open.svg" />
        <img v-else src="@/assets/images/icon/code-close.svg" />
      </span>
    </div>
    <zui-code-collapse>
      <div v-show="showCode" class="zui-code-snippet--code">
        <slot name="code"></slot>
      </div>
    </zui-code-collapse>
  </div>
</template>

<script>
import ZuiCodeCollapse from './code-collapse';

export default {
  components: { ZuiCodeCollapse },
  data() {
    return {
      showCode: false,
    };
  },
};
</script>
<style lang="scss">
.zui-code-snippet {
  position: relative;
  box-sizing: border-box;
  width: 100%;
  margin: 0 0 16px;
  border-radius: 4px;
  transition: all 0.2s;
  border: 1px solid $border;
  background-color: inherit;
  text-align: left;
  // element-ui样式冲突
  ul > li {
    list-style-type: none !important;
    margin: 0px !important;
    padding: 0px !important;
  }
  .el-pagination.is-background .el-pager li {
    margin: 0 5px !important;
  }
}
.zui-code-snippet--demo {
  box-sizing: border-box;
  padding: 30px 35px;
  color: inherit;
  border-bottom: 1px solid $border;
}
.zui-code-snippet--desc {
  position: relative;
  box-sizing: border-box;
  width: 100%;
  padding: 0 20px;
  font-size: 14px;
  transition: background-color 0.4s;
  line-height: 1.5;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.zui-code-snippet--icon-code {
  cursor: pointer;
  min-width: 18px;
  width: 18px;
  height: 18px;
  text-align: center;

  > img {
    transition: all 0.4s;
    user-select: none;
    max-width: 100%;
    width: 100%;
    vertical-align: baseline;
    box-shadow: none;
  }
}

.zui-code-snippet--code {
  box-sizing: border-box;
  border-top: 1px solid $border;

  code {
    background-color: $background;
    font-family: Consolas, Menlo, Courier, monospace;
    border: none;
    display: block;
    font-size: 14px;
    padding: 16px 32px;
    line-height: 1.5;
  }

  .hljs {
    padding: 0;
    margin: 0;

    code {
      margin: 0;
    }
  }
}
</style>
