Sveltestrap

repository·main·Indexed 42 days ago

https://github.com/sveltestrap/sveltestrap

A library providing Bootstrap 5 components for Svelte 4+ applications, allowing developers to use Bootstrap's UI patterns without manually managing JavaScript or jQuery dependencies. Version 7.1.0.

Tokens
48.6K
Snippets
237
Records
246
Agent score
51%

What's inside Sveltestrap

  1. Create multi-segment Progress bars

    main

    To stack multiple bars in a single progress container, pass the multi prop to the parent <Progress> component. The child elements should use the bar prop to indicate they are individual segments within the stack.

    <script lang="ts">
      import { Progress } from '@sveltestrap/sveltestrap';
    </script>
    
    <Progress multi>
      <Progress bar value={15} />
      <Progress bar color="success" value={30} />
      <Progress bar color="info" value={25} />
    </Progress>
  2. Use the Code Notes pattern to test Svelte slots

    main

    Because Svelte component constructors do not support slots, testing components that rely on slots can be difficult. To enable testing of slot content, use the 'Code Notes' pattern: add a conditional check for a content prop. If content is provided, render it; otherwise, render the <slot />. This allows you to pass content via a prop during unit tests instead of relying on slot injection.

    {#if content}
      {content}
    {:else}
      <slot />
    {/if}
  3. Use the InputGroup component

    main

    The <InputGroup> component is used to extend form controls by adding text, buttons, or button groups to either side of textual inputs, custom selects, and custom file inputs. It works in conjunction with <InputGroupText> and <Input> components.

    <script>
      import { InputGroup, InputGroupText, Input } from '@sveltestrap/sveltestrap';
    </script>
    
    <InputGroup>
      <InputGroupText>@example.com</InputGroupText>
      <Input placeholder="email" />
    </InputGroup>
  4. Install @sveltestrap/sveltestrap

    main

    Install the @sveltestrap/sveltestrap package along with svelte using your preferred package manager. This library provides Bootstrap 5 components for Svelte 4+ applications without requiring Bootstrap's JavaScript or jQuery.

    # npm
    npm install svelte @sveltestrap/sveltestrap
    
    # pnpm
    pnpm install svelte @sveltestrap/sveltestrap
    
    # yarn
    yarn add svelte @sveltestrap/sveltestrap
  5. Implement a responsive Navbar with Collapse and Toggler

    main

    To create a responsive Navbar that collapses on smaller screens, use the <NavbarToggler> and <Collapse> components. You must manage the visibility state (e.g., isOpen) manually in your Svelte component and listen for the on:update event from the <Collapse> component to keep your state in sync.

    <script lang="ts">
      import {
        Collapse,
        NavbarToggler,
        NavbarBrand,
        Nav,
        Navbar,
        NavItem,
        NavLink
      } from '@sveltestrap/sveltestrap';
    
      let isOpen = false;
    
      function handleUpdate(event) {
        isOpen = event.detail.isOpen;
      }
    </script>
    
    <Navbar color="light" light expand="md" container="md">
      <NavbarBrand href="/">NavBar with md container</NavbarBrand>
      <NavbarToggler on:click={() => (isOpen = !isOpen)} />
      <Collapse {isOpen} navbar expand="md" on:update={handleUpdate}>
        <Nav class="ms-auto" navbar>
          <NavItem>
            <NavLink href="#components/">Components</NavLink>
          </NavItem>
          <NavItem>
            <NavLink href="https://github.com/sveltestrap/sveltestrap">GitHub</NavLink>
          </NavItem>
        </Nav>
      </Collapse>
    </Navbar>
  6. Use the ListGroup component

    main

    The <ListGroup> component is used to display a series of content items. It is typically used in conjunction with <ListGroupItem> components. You can customize the appearance using props like color, flush, horizontal, numbered, and theme.

    <script lang="ts">
      import { ListGroup, ListGroupItem } from '@sveltestrap/sveltestrap';
    </script>
    
    <ListGroup>
      <ListGroupItem active>Active</ListGroupItem>
      <ListGroupItem>Item</ListGroupItem>
      <ListGroupItem disabled>Disabled</ListGroupItem>
    </ListGroup>
  7. Use the Accordion component

    main

    The <Accordion> component manages a collection of <AccordionItem> components to present content in a compact, organized manner.

    Basic Usage

    To create a standard accordion, wrap several <AccordionItem> components inside an <Accordion> component. You can use the active prop on an <AccordionItem> to have it open by default.

    Customizing Headers with Slots

    While <AccordionItem> accepts a header prop for simple text, you can use the slot="header" on any element inside an <AccordionItem> to provide complex HTML or custom components as the header.

    <script lang="ts">
      import { Accordion, AccordionItem } from '@sveltestrap/sveltestrap';
    </script>
    
    <Accordion>
      <AccordionItem active>
        <h4 class="m-0" slot="header">Home</h4>
        Fallbrook
      </AccordionItem>
      <AccordionItem>
        <h4 class="m-0" slot="header">School</h4>
        <a href="#home">Buena Vista Elementary</a>
      </AccordionItem>
    </Accordion>
  8. Use the Navbar component

    main

    The <Navbar> component is a responsive header element used to include branding, navigation links, and other header-related content. It works in conjunction with sub-components like <NavbarBrand>, <Nav>, <NavItem>, and <Collapse> to create a complete navigation bar.

    <script lang="ts">
      import { Navbar, NavbarBrand } from '@sveltestrap/sveltestrap';
    </script>
    
    <Navbar>
      <NavbarBrand href="/">Brand Name</NavbarBrand>
    </Navbar>
  9. Use the Pagination component

    main

    The <Pagination> component provides navigation for breaking content into manageable segments. It is used in conjunction with <PaginationItem> and <PaginationLink> to create a complete navigation bar.

    To build a pagination bar, wrap <PaginationItem> components inside <Pagination>. Each item should contain a <PaginationLink> to handle the actual navigation or interaction.

    <script lang="ts">
      import { Pagination, PaginationItem, PaginationLink } from '@sveltestrap/sveltestrap';
    </script>
    
    <Pagination ariaLabel="Page navigation example">
      <PaginationItem>
        <PaginationLink first href="#" />
      </PaginationItem>
      <PaginationItem>
        <PaginationLink previous href="#" />
      </PaginationItem>
      <PaginationItem>
        <PaginationLink href="#">1</PaginationLink>
      </PaginationItem>
      <PaginationItem active>
        <PaginationLink href="#">2</PaginationLink>
      </PaginationItem>
      <PaginationItem>
        <PaginationLink next href="#" />
      </PaginationItem>
      <PaginationItem>
        <PaginationLink last href="#" />
      </PaginationItem>
    </Pagination>
  10. Set up the local development environment

    main

    To contribute to Sveltestrap, follow these steps to clone the repository, install dependencies, and set up git hooks:

    1. Clone and configure remote:
      git clone git@github.com:<username>/sveltestrap.git
      cd sveltestrap
      git remote add upstream git@github.com:sveltestrap/sveltestrap.git
    2. Install PNPM:
      npm install -g pnpm
    3. Install husky hooks:
      pnpm dlx husky install
    4. Configure hook permissions and paths:
      chmod +x .husky/post-merge && chmod +x .husky/pre-commit && cp .husky/post-merge .git/hooks
    5. Install dependencies:
      pnpm i
    6. Start the development server (launches the documentation site):
      pnpm start

    Changes will be automatically rebuilt and reflected in the documentation immediately.

    npm install -g pnpm
    pnpm i
    pnpm dlx husky install
    chmod +x .husky/post-merge && chmod +x .husky/pre-commit && cp .husky/post-merge .git/hooks
    pnpm start
  11. Use the Carousel component

    main

    The <Carousel> component allows you to navigate through a series of images or text slides. It works by managing an activeIndex and a list of items. You can compose it with <CarouselItem> for the slides, <CarouselControl> for navigation arrows, and <CarouselIndicators> for position dots.

    To control the carousel, use bind:activeIndex on the <Carousel> component and pass the same activeIndex and items props to its sub-components.

    <script lang="ts">
      import { Carousel, CarouselItem } from '@sveltestrap/sveltestrap';
      const items = ['image1.jpg', 'image2.jpg', 'image3.jpg'];
      let activeIndex = 0;
    </script>
    
    <Carousel {items} bind:activeIndex>
      <div class="carousel-inner">
        {#each items as item, index}
          <CarouselItem bind:activeIndex itemIndex={index}>
            <img src={item} class="d-block w-100" alt="Slide {index + 1}" />
          </CarouselItem>
        {/each}
      </div>
    </Carousel>