Skip to content
Snippets Groups Projects
button.tsx 2.64 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { Component, h, Prop, Host } from "@stencil/core";
    import { ButtonVariants } from "../../utils/utils";
    import colors from "../../global/colors";
    
    const fillcolors = {
    
      primary: colors.brandMain,
    
    Markus Kaarto's avatar
    Markus Kaarto committed
      secondary: "white",
    
    };
    
    @Component({
      tag: "hy-button",
    
    Markus Kaarto's avatar
    Markus Kaarto committed
      styleUrl: "button.scss",
    
    Markus Kaarto's avatar
    Markus Kaarto committed
      shadow: false,
    
    })
    export class Button {
    
    Markus Kaarto's avatar
    Markus Kaarto committed
      /**
       * The element variant. Defaults to primary
       */
    
      @Prop() variant: ButtonVariants = "primary";
    
    Markus Kaarto's avatar
    Markus Kaarto committed
      /**
       *  deprecated, use disabled boolean value
       *  */
      @Prop() state: "enabled" | "disabled" = "enabled";
      /**
       * Size (height) of the button. Normal: 44px, large: 48px
       */
      @Prop() size: "normal" | "large" = "normal";
      /**
       * Use this to programmatically disable the button, matches the native button functionality
       */
      @Prop() disabled: boolean = false;
      /**
       *  Set this on if an anchor tag is required instead of a button.
       */
      @Prop() url?: string = "";
      /**
       * Aria label for the element
       */
      @Prop() ariaLabel?: string;
      /**
       * Use only with url property. Sets the
       */
      @Prop() isExternal?: boolean = false;
      /**
       * Icon to use on the left side
       */
    
      @Prop() icon?: string;
    
    Markus Kaarto's avatar
    Markus Kaarto committed
      /**
       * Icon to use on the right side
       */
    
      @Prop() iconRight?: string;
      render() {
    
    Markus Kaarto's avatar
    Markus Kaarto committed
        const classAttributes = [
          "hy-button",
          this.variant,
          this.state,
          "size-" + this.size,
        ];
    
    Markus Kaarto's avatar
    Markus Kaarto committed
        const iconFillColor = fillcolors[this.variant];
    
    Markus Kaarto's avatar
    Markus Kaarto committed
        const target = this.isExternal ? "_blank" : "_self";
    
        return this.url ? (
          <Host icon-right="hy-icon-arrow-right" aria-label={this.ariaLabel}>
            <div style={{ display: "flex" }}>
              <a class={classAttributes.join(" ")} href={this.url} target={target}>
                <span class="hy-button__text">
                  <slot />
                </span>
                <span class="hy-button__icon">
                  <hy-icon
                    icon="hy-icon-arrow-right"
                    fill={iconFillColor}
                    size={13}
                  />
                </span>
              </a>
            </div>
          </Host>
        ) : (
          <Host aria-label={this.ariaLabel}>
            <button class={classAttributes.join(" ")} disabled={this.disabled}>
    
    Markus Kaarto's avatar
    Markus Kaarto committed
                <span class="hy-button__icon hy-button__icon--left">
    
                  <hy-icon icon={this.icon} fill={iconFillColor} size={13} />
                </span>
              )}
    
    Markus Kaarto's avatar
    Markus Kaarto committed
              <span class="hy-button__text">
    
                <slot />
              </span>
              {!!this.iconRight && (
    
    Markus Kaarto's avatar
    Markus Kaarto committed
                <span class="hy-button__icon hy-button__icon--right">
    
                  <hy-icon icon={this.iconRight} fill={iconFillColor} size={13} />
                </span>
              )}
            </button>
          </Host>
        );
      }
    }