# Styling and Animation

Style parts and their state, then add entry and exit transitions.

Parts render without styles. The component demos use plain CSS, and the same state attributes and CSS variables work with Tailwind CSS or any other stylesheet.

## Style a Part

Every part has a `data-slot` attribute. State adds attributes such as `data-pressed` and `data-disabled`:

```ts
import { Component } from "@angular/core"
import { Toggle } from "@ui-sdk/angular/toggle"

@Component({
  selector: "app-bold-toggle",
  imports: [Toggle],
  template: `
    <button
      uiToggle
      class="rounded-md border border-neutral-300 px-2.5 py-1.5 text-sm data-pressed:bg-neutral-100 data-disabled:opacity-50"
    >
      Bold
    </button>
  `,
})
export class BoldToggle {}
```

Most state attributes are present or absent. Others have values, such as `data-side="top"`. Each component's Styling section lists its attributes and CSS variables. Use those rather than generated IDs.

`data-slot` belongs to the part. Styles and the testing helpers find parts by it, so a part keeps its own `data-slot` over one you pass. To mark an element of your own, use another attribute, such as `data-name`.

## Component Styles

The parts are elements in your own template, so your component's `styles` reach them like any other element. State selectors work the same way:

```ts
import { Component } from "@angular/core"
import { Toggle } from "@ui-sdk/angular/toggle"

@Component({
  selector: "app-bold-toggle",
  imports: [Toggle],
  template: `<button uiToggle>Bold</button>`,
  styles: `
    [data-slot="toggle"] {
      cursor: default;
      border: 1px solid var(--input, #e5e5e5);
      border-radius: 1.125rem;
      background: transparent;
      padding: 0.375rem 0.625rem;
      color: inherit;
      font: inherit;
      font-size: 0.875rem;
    }

    [data-slot="toggle"][data-pressed] {
      background: var(--muted, #f5f5f5);
    }

    [data-slot="toggle"][data-disabled] {
      opacity: 0.5;
    }

    [data-slot="toggle"]:focus-visible {
      outline: 2px solid var(--ring, #a3a3a3);
      outline-offset: 2px;
    }
  `,
})
export class BoldToggle {}
```

Popup content keeps your styles too, since it renders in place, above the page, as the same element.

## Animate Opening and Closing

Popup content gets `data-starting-style` before it enters and `data-ending-style` while it exits. It stays in the page until the exit transition or animation finishes:

```ts
import { Component } from "@angular/core"
import {
  Popover,
  PopoverClose,
  PopoverContent,
  PopoverTitle,
  PopoverTrigger,
} from "@ui-sdk/angular/popover"

@Component({
  selector: "app-project-details",
  imports: [
    Popover,
    PopoverClose,
    PopoverContent,
    PopoverTitle,
    PopoverTrigger,
  ],
  template: `
    <ng-container uiPopover>
      <button uiPopoverTrigger>Details</button>
      <div
        *uiPopoverContent
        class="origin-(--transform-origin) transition-[opacity,scale] duration-150 data-starting-style:scale-96 data-starting-style:opacity-0 data-ending-style:scale-96 data-ending-style:opacity-0 motion-reduce:transition-none"
      >
        <h2 uiPopoverTitle>Project Details</h2>
        <p>Last updated today.</p>
        <button uiPopoverClose>Done</button>
      </div>
    </ng-container>
  `,
})
export class ProjectDetails {}
```

Put exit styles on `data-ending-style`. `data-closed` describes the state but doesn't keep the element around for an exit.

An exit animation you start from script is waited for too, when it starts in the same task that closes the popup, like in a `(beforeOpenChange)` handler. One started later, from an effect or an observer, isn't.

Popups provide `--anchor-width` and `--available-height` for sizing. [Popups](/docs/angular/popups) covers mounting and placement.

## The ui-sdk Layer

A few behaviors need a little CSS, like keeping a closed popup hidden or dimming the page behind a sheet, so ui-sdk adds it to the page in a cascade layer named `ui-sdk`. Declare that layer at the top of your global stylesheet, `src/styles.css`, so your own styles win over it wherever the page puts them. With Tailwind, put it above `@import "tailwindcss"`:

```css title="src/styles.css"
@layer ui-sdk;
```

A component's `styles` load with the component, too late to come first, so keep the line in the global stylesheet.

ui-sdk adds this CSS once a component is created in the browser, so HTML rendered on the server doesn't have it until the page hydrates. Closed content kept in the page, like a tooltip's, carries `hidden`, and a class that sets `display` shows it until then. Tailwind's base styles already hide `hidden` elements. With other CSS, add the same rule to your global stylesheet:

```css title="src/styles.css"
@layer ui-sdk {
  :where([data-slot][hidden]:not([hidden="until-found"])) {
    display: none !important;
  }
}
```

It leaves `hidden="until-found"` alone, so find in page can still open a closed panel.

The same layer takes the browser's own popover border, padding and background off Popover, Dialog, Drawer, Tooltip and Hover Card content. Menu, Select, Combobox and Date Picker content keep them, so give those parts a `border`, `padding` and `background` of your own. `web.css` already does.

## Content Security Policy

A Content Security Policy without `'unsafe-inline'` in `style-src` blocks the CSS ui-sdk adds. Give ui-sdk your policy's nonce with a meta tag in the `<head>` of `src/index.html`:

```html title="src/index.html"
<meta property="csp-nonce" nonce="your-nonce" />
```

Your server replaces `your-nonce` with a new nonce on each request, the same one it passes Angular through `ngCspNonce`. If your policy has no nonce for styles, add the tag without the `nonce` attribute. ui-sdk then adds its CSS as constructed style sheets, which no policy blocks. If your code replaces `document.adoptedStyleSheets`, ui-sdk adds its sheets back the next time a component is created.

Pages rendered on the server also carry a few `style` attributes, like the one that hides a select's native input, so the policy needs `style-src-attr 'unsafe-inline'` too.
