0.1.1 • Published 4 months ago

@viamrobotics/prime-core v0.1.1

Weekly downloads
-
License
Apache-2.0
Repository
-
Last release
4 months ago

@viamrobotics/prime-core

Getting started

@viamrobotics/prime-core is a collection of core Svelte components.

Installation

Add PRIME core using your package manager of choice:

pnpm add --save-dev @viamrobotics/prime-core

Install Tailwind. In the tailwind.config.js, add the components to the content and include the theme:

import { theme } from '@viamrobotics/prime-core/theme';
import { plugins } from '@viamrobotics/prime-core/plugins';

/** @type {import('tailwindcss').Config} */
export default {
  content: [
    './src/**/*.{html,js,svelte,ts}',
    './node_modules/@viamrobotics/prime-core/**/*.{ts,svelte}',
  ],
  theme,
  plugins,
};

Import the stylesheet. If you are using SvelteKit, you can do this in src/routes/+layout.svelte.

import '@viamrobotics/prime-core/prime.css';

Usage

Once installed, you can use the components in your app:

<script lang="ts">
  import { Badge } from '@viamrobotics/prime-core';
</script>

<Badge
  variant="green"
  label="Active"
/>

Testing components that use Prime

All Prime components have their own test suites, so in your application tests, you generally only need to test that the component itself is rendered rather than try to test that all the behaviors work - we've already written those tests.

Testing toasts

The useToast hook requires a Svelte context to render. In order to test a component that issues toasts, you can use the createNoopToastContext fixture. Before using this fixture, consider if you can re-structure your components to avoid the need for testing a component wired to useToast directly.

import { describe, expect, test, vi } from 'vitest';
import { render } from '@testing-library/svelte';

import { createNoopToastContext } from '@viamrobotics/prime-core/__fixtures__';

import Subject from '../cool-component.svelte';

const toast = vi.fn();

const renderSubject = () => {
  const toastContext = createNoopToastContext(toast);
  return render(Subject, {
    props: { message: 'hello' },
    context: new Map([toastContext]),
  });
};

describe('<CoolComponent>', () => {
  it('toasts `message` on mount', () => {
    renderSubject();
    expect(toast).toHaveBeenCalledWith({ message: 'hello' });
  });
});

Playground

The playground can be used during development but is not used outside of the package.

pnpm install
pnpm -C packages/core dev

Linting

To lint and typecheck:

pnpm -C packages/core check        # check svelte and lint
pnpm -C packages/core check-svelte # check svelte only
pnpm -C packages/core check-lint   # check lint only with prettier and eslint
pnpm -C packages/core format       # format with prettier

Testing

To test with vitest:

pnpm -C packages/core test        # run once
pnpm -C packages/core test:watch  # watch tests

Anatomy of a Component

For easier readability, we try to use a standard ordering for component composition. These are not strict rules, but more a guideline to follow. Implementation specifics may force you to go outside this guideline.

<!-- svelte options: https://svelte.dev/docs/special-elements#svelte-options -->
<svelte:options immutable />

<script
  lang="ts"
  context="module"
>
  // exported types
  export type MyType = 'thing' | 'other-thing';
</script>

<script lang="ts">
  // external imports
  import { onMount, createEventDispatcher } from 'svelte';

  // internal imports
  import { someLibraryFunction } from '../parent';
  import { someSiblingFunction } from './sibling';

  // prop declarations
  /** A doc string for prop explaining what it does. */
  export let prop: MyType | undefined = undefined;

  // event dispatchers and other hooks
  const dispatcher = createEventDispatcher<{
    /** A doc string for the event handler. */
    click: null; // void event
    /** A doc string for the event handler. */
    primitive: string | number | boolean; // simple primitive values
    /** A doc string for the event handler. */
    object: { id: string; next: number; }; // complex values
    /** A doc string for the event handler. */
    native: Event // native events (for pure atoms)
  }>()

  // internal fields
  let someString = '';

  // reactive variables
  $: isThing = prop === 'thing';

  // complex reactive variables
  let counter = 0;
  $: if (isThing) {
      counter = someString.length;
      if (counter > 10) {
        counter = 10;
      }
  }

  // single-line functions
  const doSomething = () => someLibraryFunction();

  // multi-line functions
  const doSomethingElse = () => {
    const shouldDoSomething = someString !== '';
    someSiblingFunction(shouldDoSomething);
  }

  // reactive statements
  $: {
    someString = prop ? 'whoa' : 'no';
  }

  // lifecycle hooks
  onMount(() => ...);
</script>

<!-- Your layout -->
<div class="border-black">
  <!--
    all slots should be named if there are multiple; otherwise a single slot
    can be the default `<slot />`
  -->
  <slot name="title" />
  <slot name="content" />
</div>

<style>
  /* custom styles */
</style>

Tests and Test Components

We keep tests in a __tests__ directory that is a sibling of the code being tested. These directories should only contain tests and test components.

Some components require test components to render slotted children, due to limitations with rendering slots using @testing-library.

See:

0.0.175

5 months ago

0.0.179

5 months ago

0.0.178

5 months ago

0.0.177

5 months ago

0.0.176

5 months ago

0.0.180

5 months ago

0.1.0

4 months ago

0.1.1

4 months ago

0.0.174

6 months ago

0.0.173

6 months ago

0.0.172

7 months ago

0.0.171

7 months ago

0.0.170

7 months ago

0.0.169

7 months ago

0.0.168

8 months ago

0.0.167

8 months ago

0.0.166

8 months ago

0.0.165

8 months ago

0.0.164

8 months ago

0.0.163

8 months ago

0.0.162

8 months ago

0.0.161

9 months ago

0.0.159

9 months ago

0.0.158

9 months ago

0.0.157

9 months ago

0.0.156

9 months ago

0.0.155

9 months ago

0.0.160

9 months ago

0.0.153

10 months ago

0.0.152

10 months ago

0.0.151

10 months ago

0.0.150

10 months ago

0.0.154

10 months ago

0.0.149

10 months ago

0.0.148

10 months ago

0.0.147

10 months ago

0.0.146

11 months ago

0.0.145

11 months ago

0.0.144

11 months ago

0.0.143

11 months ago

0.0.142

11 months ago

0.0.141

11 months ago

0.0.140

11 months ago

0.0.139

11 months ago

0.0.138

11 months ago

0.0.137

11 months ago

0.0.136

11 months ago

0.0.135

12 months ago

0.0.134

12 months ago

0.0.128

1 year ago

0.0.127

1 year ago

0.0.126

1 year ago

0.0.125

1 year ago

0.0.129

1 year ago

0.0.124

1 year ago

0.0.131

12 months ago

0.0.130

12 months ago

0.0.133

12 months ago

0.0.132

12 months ago

0.0.123

1 year ago

0.0.122

1 year ago

0.0.120

1 year ago

0.0.121

1 year ago

0.0.119

1 year ago

0.0.118

1 year ago

0.0.117

1 year ago

0.0.116

1 year ago

0.0.115

1 year ago

0.0.114

1 year ago

0.0.113

1 year ago

0.0.109

1 year ago

0.0.112

1 year ago

0.0.111

1 year ago

0.0.110

1 year ago

0.0.108

1 year ago

0.0.106

1 year ago

0.0.107

1 year ago

0.0.105

1 year ago

0.0.104

1 year ago

0.0.103

1 year ago

0.0.102

1 year ago

0.0.101

1 year ago

0.0.100

1 year ago

0.0.99

1 year ago

0.0.98

1 year ago

0.0.96

1 year ago

0.0.97

1 year ago

0.0.95

1 year ago

0.0.94

1 year ago

0.0.93

1 year ago

0.0.91

1 year ago

0.0.92

1 year ago

0.0.90

1 year ago

0.0.89

1 year ago

0.0.88

1 year ago

0.0.86

1 year ago

0.0.87

1 year ago

0.0.85

1 year ago

0.0.84

1 year ago

0.0.82

1 year ago

0.0.83

1 year ago

0.0.81

1 year ago

0.0.80

1 year ago

0.0.79

1 year ago

0.0.77

1 year ago

0.0.78

1 year ago

0.0.76

2 years ago

0.0.74

2 years ago

0.0.75

2 years ago

0.0.73

2 years ago

0.0.71

2 years ago

0.0.72

2 years ago

0.0.70

2 years ago

0.0.69

2 years ago

0.0.68

2 years ago

0.0.67

2 years ago

0.0.66

2 years ago

0.0.64

2 years ago

0.0.63

2 years ago

0.0.62

2 years ago

0.0.61

2 years ago

0.0.60

2 years ago

0.0.59

2 years ago

0.0.58

2 years ago

0.0.57

2 years ago

0.0.56

2 years ago

0.0.55

2 years ago

0.0.54

2 years ago

0.0.53

2 years ago

0.0.52

2 years ago

0.0.51

2 years ago

0.0.50

2 years ago

0.0.49

2 years ago

0.0.48

2 years ago

0.0.47

2 years ago

0.0.46

2 years ago

0.0.45

2 years ago

0.0.44

2 years ago

0.0.43

2 years ago

0.0.42

2 years ago

0.0.41

2 years ago

0.0.40

2 years ago

0.0.39

2 years ago

0.0.38

2 years ago

0.0.37

2 years ago

0.0.36

2 years ago

0.0.35

2 years ago

0.0.34

2 years ago

0.0.33

2 years ago

0.0.32

2 years ago

0.0.31

2 years ago

0.0.30

2 years ago

0.0.29

2 years ago

0.0.28

2 years ago

0.0.27

2 years ago

0.0.26

2 years ago

0.0.25

2 years ago

0.0.24

2 years ago

0.0.23

2 years ago

0.0.22

2 years ago

0.0.21

2 years ago

0.0.20

2 years ago

0.0.19

2 years ago

0.0.18

2 years ago

0.0.17

2 years ago

0.0.16

2 years ago

0.0.15

2 years ago

0.0.14

2 years ago

0.0.13

2 years ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago