Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

3.8 KB
Raw Blame
import { isTouchDevice } from '@core/mobile/isTouchDevice';
import {
  SegmentedControl as KSegmentedControl,
  type SegmentedControlRootProps,
  useSegmentedControlContext,
} from '@kobalte/core/segmented-control';
import { createResizeObserver } from '@solid-primitives/resize-observer';
import { cn } from '@ui';
import {
  batch,
  type ComponentProps,
  createEffect,
  createSignal,
  For,
  type JSX,
  on,
  splitProps,
} from 'solid-js';

export type UnderlineTabItem = {
  value: string;
  label: string | JSX.Element;
};

/** The mobile share sheet's tab bar; everything else uses `Tabs` from `@ui`. */
export const UnderlineTabs = (
  props: {
    list: UnderlineTabItem[];
    value?: string;
    defaultValue?: string;
    indicatorPosition?: 'top' | 'bottom';
    class?: string;
  } & Omit<SegmentedControlRootProps, 'defaultValue'>
) => {
  const [local, rootProps] = splitProps(props, [
    'list',
    'value',
    'defaultValue',
    'disabled',
    'indicatorPosition',
    'class',
  ]);

  return (
    <KSegmentedControl
      value={local.value}
      defaultValue={local.defaultValue ?? local.list[0]?.value}
      disabled={local.disabled}
      {...rootProps}
      class={cn('h-full', local.class)}
    >
      <div class="relative flex items-center h-full">
        <For each={local.list}>
          {(item) => (
            <KSegmentedControl.Item
              value={item.value}
              disabled={local.disabled}
            >
              <KSegmentedControl.ItemInput class="absolute inset-0 pointer-events-none" />
              <KSegmentedControl.ItemLabel
                class={cn(
                  'flex items-center px-2 py-1 text-sm font-medium',
                  'text-ink-extra-muted',
                  'data-checked:text-accent hover:text-accent'
                )}
                // We don't want touches on touch to unfocus inputs
                onPointerDown={(e) => {
                  if (isTouchDevice()) e.preventDefault();
                }}
                onClick={() => {
                  rootProps.onChange?.(item.value);
                }}
              >
                {item.label}
              </KSegmentedControl.ItemLabel>
            </KSegmentedControl.Item>
          )}
        </For>
        <Indicator
          data-indicator
          class={cn(
            'absolute h-0.5! bg-accent transition-[transform,width] duration-150 pointer-events-none',
            (local.indicatorPosition ?? 'bottom') === 'top'
              ? 'top-0'
              : 'bottom-0'
          )}
        />
      </div>
    </KSegmentedControl>
  );
};

// This is based off of the KSegmentedControl.Indicator but removes the
// height and Y translation styles
const Indicator = (props: ComponentProps<'div'>) => {
  const context = useSegmentedControlContext();

  const [style, setStyle] = createSignal<JSX.CSSProperties>();
  const [resizing, setResizing] = createSignal(false);

  const computeTransform = (element: HTMLElement): string | undefined => {
    const x = element.offsetLeft;

    return `translateX(${x}px)`;
  };

  const computeStyle = () => {
    const element = context.selectedItem();

    if (!element) {
      setStyle(undefined);
      return;
    }

    setStyle({
      width: `${element.offsetWidth}px`,
      transform: computeTransform(element),
      'transition-duration': resizing() ? '0ms' : undefined,
    });
  };

  createEffect(
    on(context.selectedItem, () => {
      setResizing(!style());
      computeStyle();
      setResizing(false);
    })
  );

  createResizeObserver(context.root, () => {
    batch(() => {
      setResizing(true);
      computeStyle();
      setResizing(false);
    });
  });

  return (
    <div
      role="presentation"
      style={style()}
      data-resizing={resizing()}
      data-orientation={context.orientation()}
      {...props}
    />
  );
};