typescript 一种基于一组主题来对组件进行主题化的方法。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了typescript 一种基于一组主题来对组件进行主题化的方法。相关的知识,希望对你有一定的参考价值。

$theme-and-color: (
  'primary': $color__primary,
  'accent': $color__accent,
  'error': $color__error,
  'warn': $color__warn,
  'highlight': $color__highlight,
  'pure': $color__pure--light,
  'utility': $color__utility
);

/**
 * Output conditional styles for the possible themes
 *
 * @mixin theme-color
 * @section Functions
 * @param $property
 *  The CSS property that should be colored. Default: 'background-color'
 * @example
 *  @include theme-color;
 *  @include theme-color('color');
 */
@mixin theme-color($property: 'background-color') {
  // Verify an allowed property was passed in
  @if not(($property == background-color)) and not(($property == color)) {
    @error 'The `theme__color` mixin only accepts `background-color` or `color`';
  }

  @each $key, $value in $theme-and-color {
    ::ng-deep .u-theme--#{$key} & {
      #{$property}: $value;

      @if ($property == 'background-color') and (not(($key == 'pure')) and not(($key == 'highlight'))) {
        color: $color__pure--light;
      }

      @if ($property == 'background-color') and ($key == 'highlight') {
        color: $color__pure--dark;
      }

      &[disabled] {
        background-color: color(utility, light);
        color: color(utility);
      }
    }
  }
}
import {
  Component,
  Input,
  ElementRef,
} from '@angular/core';

import { TsStyleThemeTypes } from './../types/style-theme.types';


/**
 * A base class to set a class theme on a component
 */
@Component({
  selector: 'ts-theme-base',
})
export class TsThemeBaseComponent {
  /**
   * Define the button style
   */
  @Input() set theme(theme: TsStyleThemeTypes) {
    if (!theme) {
      return;
    }

    this.element.nativeElement.classList.add(`u-theme--${theme}`);
  };

  constructor(
    protected element: ElementRef,
  ) {}

}

// Global material overrides
::ng-deep .mat-option.mat-selected.mat-primary,
::ng-deep .mat-primary .mat-option.mat-selected {
  color: color(primary);
}

// sass-lint:disable force-pseudo-nesting
::ng-deep .mat-select:focus:not(.mat-select-disabled).mat-primary .mat-select-trigger {
  color: color(primary);
}

::ng-deep .mat-primary .mat-pseudo-checkbox-checked,
::ng-deep .mat-primary .mat-pseudo-checkbox-indeterminate,
::ng-deep .mat-pseudo-checkbox-checked.mat-primary,
::ng-deep .mat-pseudo-checkbox-indeterminate.mat-primary {
  background: color(primary);
}
// sass-lint:enable force-pseudo-nesting

// Global material overrides
::ng-deep .mat-option.mat-selected.mat-primary,
::ng-deep .mat-primary .mat-option.mat-selected {
  color: color(primary);
}

// sass-lint:disable force-pseudo-nesting
::ng-deep .mat-select:focus:not(.mat-select-disabled).mat-primary .mat-select-trigger {
  color: color(primary);
}
// sass-lint:enable force-pseudo-nesting
// Global material overrides
::ng-deep .mat-focused .mat-input-placeholder {
  color: color(primary);
}

::ng-deep .mat-input-ripple {
  background-color: color(primary);
}
// Global material overrides
::ng-deep .mat-calendar-body-selected {
  background-color: color(primary);
}
import {
  Component,
  Input,
  Output,
  EventEmitter,
  ElementRef,
} from '@angular/core';

import { ButtonActionTypes } from './../types/button-action.types';
import { ButtonFunctionTypes } from './../types/button-function.types';
import { TsThemeBaseComponent } from './../utilities/theme-base.component';


/**
 * A presentational component to render a button
 * TODO: Text color transition doesn't match the background transition timing
 *
 * @example
 * <ts-button
 *              actionName="Submit"
 *              theme="primary"
 *              buttonType="search"
 *              iconName="search"
 *              isDisabled="false"
 *              showProgress="true"
 *              (clickEvent)="myMethod($event)
 * ></ts-button>
 */
@Component({
  selector: 'ts-button',
  templateUrl: './button.component.html',
  styleUrls: ['./button.component.scss'],
})
export class TsButtonComponent extends TsThemeBaseComponent {
  /**
   * Define the action for the aria-label
   */
  @Input() actionName: ButtonActionTypes = 'Button';

  /**
   * Define the button type
   */
  @Input() buttonType: ButtonFunctionTypes = 'button';

  /**
   * Define a Material icon to include
   */
  @Input() iconName: string;

  /**
   * Define if the button is disabled
   */
  @Input() isDisabled: boolean = false;

  /**
   * Define if the progress indicator should show
   */
  @Input() showProgress: boolean = false;

  /**
   * Pass the click event through to the parent
   */
  @Output() clickEvent = new EventEmitter<any>();

  constructor(
    protected element: ElementRef,
  ) {
    super(element);
  }

}

以上是关于typescript 一种基于一组主题来对组件进行主题化的方法。的主要内容,如果未能解决你的问题,请参考以下文章

包括对带有 typescript 的样式化组件主题的媒体查询

typescript 创建一个将在组件上设置主题类的基类。

无法更改样式化组件主题提供程序

解决基于TypeScript 的 RN项目相对路径引入组件的问题

TypeScript Interface(接口)

vue组件库基于@vue/cli构建typescript版UI库 -组件文档网站