Sha256: d990c24c0fe64931236af24b8725d6e111c2ad47e79489367a0609ab6ddd8af2

Contents?: true

Size: 1.28 KB

Versions: 143

Compression:

Stored size: 1.28 KB

Contents

import {controller, targets} from '@github/catalyst'

@controller
class SegmentedControlElement extends HTMLElement {
  @targets items: HTMLElement[]

  connectedCallback() {
    this.#updateButtonLabels()
  }

  select(event: Event) {
    const button = event.currentTarget as HTMLButtonElement
    for (const item of this.items) {
      item.classList.remove('SegmentedControl-item--selected')
      item.querySelector('[aria-current]')?.setAttribute('aria-current', 'false')
    }

    button.closest('li.SegmentedControl-item')?.classList.add('SegmentedControl-item--selected')
    button.setAttribute('aria-current', 'true')
  }

  // Updates the button labels to have a data-content attribute with the text
  // This is for selection styling to avoid the text jumping. It only needs to be
  // setup when the component is first loaded.
  #updateButtonLabels() {
    for (const label of this.querySelectorAll('.Button-label')) {
      label.setAttribute('data-content', label.textContent || '')
    }
  }
}

declare global {
  interface Window {
    SegmentedControlElement: typeof SegmentedControlElement
  }
}

if (!window.customElements.get('segmented-control')) {
  window.SegmentedControlElement = SegmentedControlElement
  window.customElements.define('segmented-control', SegmentedControlElement)
}

Version data entries

143 entries across 143 versions & 3 rubygems

Version Path
primer_view_components-0.35.2 app/components/primer/alpha/segmented_control.ts
openproject-primer_view_components-0.49.1 app/components/primer/alpha/segmented_control.ts
openproject-primer_view_components-0.49.0 app/components/primer/alpha/segmented_control.ts
primer_view_components-0.35.1 app/components/primer/alpha/segmented_control.ts
primer_view_components-0.35.0 app/components/primer/alpha/segmented_control.ts
openproject-primer_view_components-0.48.2 app/components/primer/alpha/segmented_control.ts
openproject-primer_view_components-0.48.1 app/components/primer/alpha/segmented_control.ts
openproject-primer_view_components-0.48.0 app/components/primer/alpha/segmented_control.ts
openproject-primer_view_components-0.47.1 app/components/primer/alpha/segmented_control.ts
openproject-primer_view_components-0.47.0 app/components/primer/alpha/segmented_control.ts
primer_view_components-0.34.0 app/components/primer/alpha/segmented_control.ts
openproject-primer_view_components-0.46.1 app/components/primer/alpha/segmented_control.ts
openproject-primer_view_components-0.46.0 app/components/primer/alpha/segmented_control.ts
openproject-primer_view_components-0.45.0 app/components/primer/alpha/segmented_control.ts
openproject-primer_view_components-0.44.3 app/components/primer/alpha/segmented_control.ts
openproject-primer_view_components-0.44.2 app/components/primer/alpha/segmented_control.ts
openproject-primer_view_components-0.44.1 app/components/primer/alpha/segmented_control.ts
primer_view_components-0.33.0 app/components/primer/alpha/segmented_control.ts
openproject-primer_view_components-0.44.0 app/components/primer/alpha/segmented_control.ts
openproject-primer_view_components-0.43.1 app/components/primer/alpha/segmented_control.ts