Skip to content

Component Documentation

This document provides documentation for the UI components used in the Elome Frontend project.

Overview

The Elome Frontend uses Vue.js components organized by feature and following atomic design principles. Components are located in the components/ directory and are organized into subdirectories based on their functionality.

Component Organization

Components are organized into the following categories:

  • Feature-based components: Located in feature-specific directories (e.g., cart/, product/)
  • UI components: Reusable UI elements located in the ui/ directory
  • Layout components: Components that define the layout of the application

Cart Components

FreeShippingNotice

Displays a notice about free shipping eligibility based on the cart total.

Location: components/cart/FreeShippingNotice.vue

Props: None (uses composables for data)

Usage:

<template>
  <FreeShippingNotice />
</template>

Behavior:

  • Shows a success message when free shipping is unlocked
  • Shows the remaining amount needed to qualify for free shipping when not unlocked
  • Conditionally renders based on whether free shipping is enabled and the cart has items

Overlay

Displays the cart overlay with cart items, totals, and checkout button.

Location: components/cart/Overlay.vue

Props: None (uses stores for data)

Usage:

<template>
  <Overlay />
</template>

Behavior:

  • Shows a list of cart items
  • Displays cart totals including subtotal, shipping, and total
  • Provides buttons for checkout and continuing shopping
  • Can be toggled open/closed

UI Components

The UI components follow atomic design principles and are organized into:

  • Atoms: Basic building blocks (buttons, inputs, icons)
  • Molecules: Groups of atoms functioning together (form fields, search bars)
  • Organisms: Complex UI components composed of molecules and atoms (navigation menus, product cards)

UiText

A text component with various typography styles.

Location: components/ui/atoms/UiText.vue

Props:

  • variant (String): The typography variant to use (e.g., 'h1', 'h2', 'b1', 'b2')
  • tag (String): The HTML tag to use (defaults based on variant)

Usage:

<template>
  <UiText variant="h1">Heading 1</UiText>
  <UiText variant="b1" tag="span">Body text</UiText>
</template>

Component Best Practices

  1. Props Validation: Always define props with proper validation

    props: {
      variant: {
        type: String,
        required: true,
        validator: (value: string) => ['h1', 'h2', 'b1', 'b2'].includes(value)
      }
    }
    

  2. Composables: Use composables for reusable logic

    const { formatPrice } = usePriceFormatter();
    

  3. Emit Events: Use emit for component communication

    const emit = defineEmits<{
      (e: 'update:modelValue', value: string): void
      (e: 'submit'): void
    }>();
    

  4. Slots: Use slots for flexible content

    <template>
      <div class="card">
        <div class="card-header">
          <slot name="header">Default Header</slot>
        </div>
        <div class="card-body">
          <slot></slot>
        </div>
        <div class="card-footer">
          <slot name="footer"></slot>
        </div>
      </div>
    </template>
    

  5. Styling: Use scoped styles and CSS variables

    <style scoped>
    .component {
      color: var(--color-primary);
      padding: var(--spacing-md);
    }
    </style>
    

Creating New Components

When creating new components:

  1. Place the component in the appropriate directory based on its functionality
  2. Use the .vue extension
  3. Include proper documentation using JSDoc comments
  4. Define props with validation
  5. Use TypeScript for type safety
  6. Include unit tests in the tests/components/ directory

Example Component Structure:

<script lang="ts" setup>
/**
 * Button component with various styles and sizes
 */
interface Props {
  /**
   * The button variant
   * @default 'primary'
   */
  variant?: 'primary' | 'secondary' | 'tertiary';

  /**
   * The button size
   * @default 'md'
   */
  size?: 'sm' | 'md' | 'lg';

  /**
   * Whether the button is disabled
   * @default false
   */
  disabled?: boolean;
}

const props = withDefaults(defineProps<Props>(), {
  variant: 'primary',
  size: 'md',
  disabled: false
});

const emit = defineEmits<{
  (e: 'click', event: MouseEvent): void
}>();

const handleClick = (event: MouseEvent) => {
  if (!props.disabled) {
    emit('click', event);
  }
};
</script>

<template>
  <button
    :class="[`btn-${variant}`, `btn-${size}`, { 'btn-disabled': disabled }]"
    :disabled="disabled"
    @click="handleClick"
  >
    <slot></slot>
  </button>
</template>

<style scoped>
.btn-primary {
  background-color: var(--color-primary);
  color: white;
}

.btn-secondary {
  background-color: var(--color-secondary);
  color: white;
}

.btn-tertiary {
  background-color: transparent;
  color: var(--color-primary);
}

.btn-sm {
  padding: 0.25rem 0.5rem;
  font-size: 0.875rem;
}

.btn-md {
  padding: 0.5rem 1rem;
  font-size: 1rem;
}

.btn-lg {
  padding: 0.75rem 1.5rem;
  font-size: 1.125rem;
}

.btn-disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
</style>