File

src/components/sq-button/sq-button.component.ts

Description

Represents the SqButtonComponent, a customizable button component.

Look the link about the component in original framework and the appearance

See https://css.squidit.com.br/components/button


Example :
<sq-button type="button" color="primary" [loading]="false" (emitClick)="onClick($event)">
  Click Me
</sq-button>

Metadata

Index

Properties
Methods
Inputs
Outputs

Constructor

constructor(element: ElementRef, colorsHelper: ColorsHelper)

Constructor for the SqButtonComponent class.

Parameters :
Name Type Optional Description
element ElementRef No
  • The ElementRef representing the button element.
colorsHelper ColorsHelper No
  • The ColorsHelper service for color manipulation.

Inputs

block
Type : boolean

Indicates whether the button should occupy the full width of its container.

borderColor
Type : string
Default value : ''

The border color of the button.

borderRadius
Type : string
Default value : ''

The border radius of the button.

borderStyle
Type : string
Default value : ''

The border style of the button.

borderWidth
Type : string
Default value : ''

The width of the button border.

boxShadow
Type : string

The box shadow of the button.

buttonAsLink
Type : boolean

Indicates whether the button should be styled as a link.

color
Type : string
Default value : 'primary'

The background color of the button.

disabled
Type : boolean

Indicates whether the button is disabled.

fontSize
Type : string

The font size of the button.

hideOnLoading
Type : boolean

Indicates whether the button should be hidden during the loading state.

id
Type : string

The ID attribute of the button.

invertedHover
Type : boolean
Default value : false

Indicates whether the button should have inverted hover colors.

loading
Type : boolean

Indicates whether the button is in a loading state.

noPadding
Type : boolean

Indicates whether to remove padding from the button.

noUnderline
Type : boolean

Indicates whether the button text should have no underline.

size
Type : "sm" | "md" | "lg" | "xl"
Default value : 'md'

The size of the button ('sm', 'md', 'lg', 'xl').

textColor
Type : string
Default value : ''

The text color of the button.

textTransform
Type : string
Default value : ''

The text transformation of the button text (e.g., 'uppercase', 'lowercase').

type
Type : "submit" | "reset" | "button"
Default value : 'button'

The type of the button (e.g., 'button', 'submit', 'reset').

width
Type : string

The width of the button.

Outputs

emitClick
Type : EventEmitter<MouseEvent>

Event emitter for when the button is clicked.

Methods

doHoverOnBackground
doHoverOnBackground()

Determines the background color when hovering.

Returns : any

The background color when hovering.

doHoverOnBorder
doHoverOnBorder()

Determines the border color when hovering.

Returns : any

The border color when hovering.

doHoverOnText
doHoverOnText()

Determines the text color when hovering.

Returns : any

The text color when hovering.

executeFunction
executeFunction(event: MouseEvent)

Executes a function when the button is clicked.

Parameters :
Name Type Optional Description
event MouseEvent No
  • The MouseEvent associated with the click event.
Returns : void
setHover
setHover(color: string)

Sets the hover color.

Parameters :
Name Type Optional Description
color string No
  • The color to apply the hover effect.
Returns : any

The color with the hover effect applied.

setHoverBg
setHoverBg()

Sets the hover background color.

Returns : any

The background color with the hover effect applied.

setHoverText
setHoverText()

Sets the hover text color.

Returns : any

The text color with the hover effect applied.

validatePresetColors
validatePresetColors()

Validates if the preset colors are set correctly.

Returns : boolean

True if the color is set correctly; otherwise, false.

Properties

Public colorsHelper
Type : ColorsHelper
- The ColorsHelper service for color manipulation.
doHoverAction
Default value : useMemo((type: 'text' | 'background' | 'border') => { if (this.validatePresetColors()) { return '' } switch (type) { case 'text': return this.doHoverOnText() case 'background': return this.doHoverOnBackground() case 'border': return this.doHoverOnBorder() default: return '' } })

Performs hover-specific actions based on the type (text, background, border).

Parameters :
Name Description
type
  • The type of hover action.
Public element
Type : ElementRef
- The ElementRef representing the button element.
hover
Default value : false

Indicates whether the mouse is hovering over the button.

nativeElement
Type : ElementRef

The native element of the button.

import { Component, ElementRef, EventEmitter, Input, Output } from '@angular/core'
import { ColorsHelper } from '../../helpers/colors.helper'
import { useMemo } from '../../helpers/memo.helper'

/**
 * Represents the SqButtonComponent, a customizable button component.
 * 
 * Look the link about the component in original framework and the appearance
 * 
 * @see {@link https://css.squidit.com.br/components/button}
 * 
 * <br>
 * <button type="button" class='button button-primary mb-3'>Click Me</button>
 * 
 * @example
 * <sq-button type="button" color="primary" [loading]="false" (emitClick)="onClick($event)">
 *   Click Me
 * </sq-button>
 */
@Component({
  selector: 'sq-button',
  templateUrl: './sq-button.component.html',
  styleUrls: ['./sq-button.component.scss'],
})
export class SqButtonComponent {
  /**
   * The type of the button (e.g., 'button', 'submit', 'reset').
   */
  @Input() type: 'submit' | 'reset' | 'button' = 'button'

  /**
   * The background color of the button.
   */
  @Input() color = 'primary'

  /**
   * The text color of the button.
   */
  @Input() textColor = ''

  /**
   * The border color of the button.
   */
  @Input() borderColor = ''

  /**
   * The border style of the button.
   */
  @Input() borderStyle = ''

  /**
   * The text transformation of the button text (e.g., 'uppercase', 'lowercase').
   */
  @Input() textTransform = ''

  /**
   * The width of the button border.
   */
  @Input() borderWidth = ''

  /**
   * The border radius of the button.
   */
  @Input() borderRadius = ''

  /**
   * The size of the button ('sm', 'md', 'lg', 'xl').
   */
  @Input() size: 'sm' | 'md' | 'lg' | 'xl' = 'md'

  /**
   * The font size of the button.
   */
  @Input() fontSize?: string

  /**
   * Indicates whether the button is in a loading state.
   */
  @Input() loading?: boolean

  /**
   * Indicates whether the button is disabled.
   */
  @Input() disabled?: boolean

  /**
   * Indicates whether the button should occupy the full width of its container.
   */
  @Input() block?: boolean

  /**
   * Indicates whether to remove padding from the button.
   */
  @Input() noPadding?: boolean

  /**
   * The ID attribute of the button.
   */
  @Input() id?: string

  /**
   * Indicates whether the button should be styled as a link.
   */
  @Input() buttonAsLink?: boolean

  /**
   * Indicates whether the button should be hidden during the loading state.
   */
  @Input() hideOnLoading?: boolean

  /**
   * Indicates whether the button should have inverted hover colors.
   */
  @Input() invertedHover = false

  /**
   * Indicates whether the button text should have no underline.
   */
  @Input() noUnderline?: boolean

  /**
   * The box shadow of the button.
   */
  @Input() boxShadow?: string

  /**
   * The width of the button.
   */
  @Input() width?: string

  /**
   * Event emitter for when the button is clicked.
   */
  @Output() emitClick: EventEmitter<MouseEvent> = new EventEmitter()

  /**
   * The native element of the button.
   */
  nativeElement: ElementRef

  /**
   * Indicates whether the mouse is hovering over the button.
   */
  hover = false

  /**
   * Constructor for the SqButtonComponent class.
   * @param element - The ElementRef representing the button element.
   * @param colorsHelper - The ColorsHelper service for color manipulation.
   */
  constructor(public element: ElementRef, public colorsHelper: ColorsHelper) {
    this.nativeElement = element.nativeElement
  }

  /**
   * Validates if the preset colors are set correctly.
   * @returns True if the color is set correctly; otherwise, false.
   */
  validatePresetColors() {
    if (
      !this.color.startsWith('var(--') &&
      !this.color.startsWith('#') &&
      !this.color.startsWith('rgb') &&
      !this.color.startsWith('hsl') &&
      !this.color.startsWith('transparent')
    ) {
      return !!this.colorsHelper?.getCssVariableValue(this.color)
    }
    return false
  }

  /**
   * Determines the text color when hovering.
   * @returns The text color when hovering.
   */
  doHoverOnText() {
    if (this.hover) {
      return this.setHoverText()
    }
    return this.textColor
  }

  /**
   * Determines the background color when hovering.
   * @returns The background color when hovering.
   */
  doHoverOnBackground() {
    if (this.hover) {
      return this.setHoverBg()
    }
    return this.color
  }

  /**
   * Determines the border color when hovering.
   * @returns The border color when hovering.
   */
  doHoverOnBorder() {
    if (this.hover) {
      return this.setHover(this.borderColor || this.textColor || '')
    }
    return this.borderColor || this.textColor || ''
  }

  /**
   * Performs hover-specific actions based on the type (text, background, border).
   * @param type - The type of hover action.
   * @returns The resulting hover action value.
   */
  doHoverAction = useMemo((type: 'text' | 'background' | 'border') => {
    if (this.validatePresetColors()) {
      return ''
    }

    switch (type) {
      case 'text':
        return this.doHoverOnText()
      case 'background':
        return this.doHoverOnBackground()
      case 'border':
        return this.doHoverOnBorder()
      default:
        return ''
    }
  })

  /**
   * Sets the hover color.
   * @param color - The color to apply the hover effect.
   * @returns The color with the hover effect applied.
   */
  setHover(color: string) {
    return this.colorsHelper?.lightenDarkenColor(this.colorsHelper?.getCssVariableValue(color), -25)
  }

  /**
   * Sets the hover background color.
   * @returns The background color with the hover effect applied.
   */
  setHoverBg() {
    if (this.invertedHover) {
      return this.setHover(this.textColor !== 'transparent' ? this.textColor || '' : 'var(--text_color)')
    }
    return this.setHover(this.color !== 'transparent' ? this.color : 'var(--color_bg_button_inverse-hover)')
  }

  /**
   * Sets the hover text color.
   * @returns The text color with the hover effect applied.
   */
  setHoverText() {
    if (this.invertedHover) {
      return this.setHover(this.color !== 'transparent' ? this.color : 'var(--color_bg_button_inverse-hover)')
    }
    return this.setHover(this.textColor !== 'transparent' ? this.textColor || '' : 'var(--text_color)')
  }

  /**
   * Executes a function when the button is clicked.
   * @param event - The MouseEvent associated with the click event.
   */
  executeFunction(event: MouseEvent) {
    const { loading, disabled } = this
    if (!loading && !disabled) {
      this.emitClick.emit(event)
    }
  }
}
<button
  id="{{ id }}"
  class="button button-{{ color }} button-{{ size }}"
  [ngStyle]="{
    'font-size': fontSize,
    color: doHoverAction('text'),
    'background-color': doHoverAction('background'),
    'border-color': doHoverAction('border'),
    'border-style': borderStyle,
    'border-radius': borderRadius,
    'border-width': borderWidth,
    'box-shadow': boxShadow,
    width: width,
    'text-transform': textTransform
  }"
  [ngClass]="{
    disabled: disabled,
    loading: loading,
    block: block,
    'p-0': noPadding,
    'button-as-link': buttonAsLink,
    'no-underline': noUnderline,
    'inverted': invertedHover
  }"
  [type]="type || 'button'"
  (click)="executeFunction($event)"
  (mouseover)="hover = true"
  (mouseleave)="hover = false"
  [disabled]="disabled"
>
  <ng-content *ngIf="!hideOnLoading || (hideOnLoading && !loading)"></ng-content>
  <sq-loader
    class="{{ hideOnLoading ? 'hide-on-loading' : '' }}"
    *ngIf="loading"
    customSize="1rem"
    color="{{ textColor }}"
  ></sq-loader>
</button>

./sq-button.component.scss

.button {
  &.p-0 {
    padding: 0;
  }
  &.block {
    width: 100%;
  }
  &.button-as-link {
    border-style: none !important;
    background-color: transparent !important;
    text-decoration: underline;
    color: initial !important;
    white-space: nowrap;
    text-transform: none;
  }
  &.no-underline {
    text-decoration: none;
  }
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""