Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/gossi/ember-command. Integrate your Business Logic with Ember gossi.github.io/ember-command
Something went wrong. Try again.
2.1 kB · 86 lines
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687import Component from '@glimmer/component';import { on } from '@ember/modifier';import { action } from '@ember/object';
import { element } from 'ember-element-helper';
import { getLink } from '../-private/instance.ts';
import type { AnyFunction } from '../-private/action.ts';import type { CommandAction } from '../-private/instance.ts';import type { ElementFromTagName, ElementSignature } from 'ember-element-helper';import type { Link } from 'ember-link';
export interface CommandSignature<T extends string = 'span'> { Element: HTMLButtonElement | HTMLAnchorElement | ElementFromTagName<T>; Args: { command?: CommandAction; /** * Pass in a `(element)` as fallback when `@command` is empty. Anyway a `<span>` * is used. */ element?: ElementSignature<'a' | 'button' | T>['Return']; }; Blocks: { default: []; };}
export default class CommandElement extends Component<CommandSignature> { get tagName(): 'a' | 'button' | undefined { if (this.link) { return 'a'; }
if (this.command) { return 'button'; }
return undefined; }
get command(): AnyFunction | undefined { if (typeof this.args.command === 'function') { return this.args.command; }
return undefined; }
get link(): Link | undefined { return this.args.command && getLink(this.args.command); }
@action invoke(event: Event): void { if (typeof this.args.command === 'function') { void this.args.command(); }
if (this.link) { // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition if (this.link.open) { this.link.open(event); } else { this.link.transitionTo(event); } } }
<template> {{#let (if this.tagName (element this.tagName) (if @element @element (element "span"))) as |Element| }} <Element href={{if this.link this.link.url}} type={{if this.command "button"}} {{on "click" this.invoke}} data-test-commander ...attributes > {{yield}} </Element> {{/let}} </template>}