# Development-only code

> Write `if (isDev)` to warn a component's author in development. Each framework gets its own check, and a production build drops the code.

Development-only code lets a component tell its author about a mistake without shipping the message to production, like a button with no accessible name, a prop out of range or a slot left empty. In Unframework you write `if (isDev) { … }`, and each framework gets its own development check, which its production build folds to `false` and drops. Think of a design system that warns in every app that uses it while it's developed, and adds nothing to any of them once it's built.

This page walks you through writing it. We'll look at:

- **A setup check:** a warning from the props, where the setup runs.
- **A client check:** a warning from the rendered DOM, once mounted.
- **What the compiler checks:** where `isDev` may go.
- **What your app does:** the one thing a production server sets.

## A setup check

Import `isDev` from `@uxfront/unframework`, and write `if (isDev) { … }` at the top of the setup. The block runs once where the setup runs, and reads what the setup reads, like the props, plus `console`:

<!-- prettier-ignore -->
```tsx [IconButton.uf.tsx]
import { isDev } from "@uxfront/unframework";

// A button that shows only an icon, so its `label` is its accessible name. A development build
// warns its author when it has none, and a production build drops the check (ADR-0117).
export default function IconButton({ label }: { label?: string }) {
  if (isDev) {
    if (!label) console.warn("IconButton has no accessible name: pass it a `label`.");
  }
  return (
    <button type="button" aria-label={label}>
      <svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true">
        <path d="M8 1l2 5h5l-4 3 2 6-5-4-5 4 2-6-4-3h5z" />
      </svg>
    </button>
  );
}
```

React, Vue and Svelte test `process.env.NODE_ENV`, which the app's bundler replaces. Solid and Qwik import their own `isDev`, which their production build exports as `false`. Angular tests the global `ngDevMode`, and Astro tests `import.meta.env.DEV`, which Vite replaces. Each framework runs the block where it runs the setup: React in the body, Vue in `<script setup>`, Svelte in its script, Solid and Qwik in the body, Angular in `ngOnInit` and Astro in the frontmatter. Svelte, Solid and Qwik read the props there untracked, so the block subscribes to nothing:

::framework-switcher

#react

<!-- prettier-ignore -->
```tsx [IconButton.tsx]
import type { ComponentPropsWithRef } from "react";

export type IconButtonAttributes = Omit<ComponentPropsWithRef<"button">, "label" | "children">;

export default function IconButton({
  label,
  className,
  style,
  ...attributes
}: { label?: string } & IconButtonAttributes) {
  if (process.env.NODE_ENV !== "production") {
    if (!label) console.warn("IconButton has no accessible name: pass it a `label`.");
  }

  return (
    <button type="button" aria-label={label} className={className} style={style} {...attributes}>
      <svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true">
        <path d="M8 1l2 5h5l-4 3 2 6-5-4-5 4 2-6-4-3h5z" />
      </svg>
    </button>
  );
}
```

#vue

<!-- prettier-ignore -->
```vue [IconButton.vue]
<script setup lang="ts">
const { label = undefined } = defineProps<{ label?: string }>();

if (process.env.NODE_ENV !== "production") {
  if (!label) console.warn("IconButton has no accessible name: pass it a `label`.");
}
</script>

<template>
  <button type="button" :aria-label="label">
    <svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true">
      <path d="M8 1l2 5h5l-4 3 2 6-5-4-5 4 2-6-4-3h5z" />
    </svg>
  </button>
</template>
```

#svelte

<!-- prettier-ignore -->
```svelte [IconButton.svelte]
<svelte:options runes={true} preserveWhitespace={false} />

<script lang="ts">
  import { untrack } from "svelte";
  import type { ClassValue, HTMLButtonAttributes } from "svelte/elements";

  type Props = Omit<HTMLButtonAttributes, "label" | "children" | "class" | "style" | "ref"> & {
    label?: string;
    class?: ClassValue;
    style?: string;
    ref?: HTMLElementTagNameMap["button"] | null;
  };

  let { label, class: className, style, ref = $bindable(null), ...attributes }: Props = $props();

  untrack(() => {
    if (process.env.NODE_ENV !== "production") {
      if (!label) console.warn("IconButton has no accessible name: pass it a `label`.");
    }
  });
</script>

<button type="button" aria-label={label} class={className} {style} bind:this={ref} {...attributes}>
  <svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true">
    <path d="M8 1l2 5h5l-4 3 2 6-5-4-5 4 2-6-4-3h5z" />
  </svg>
</button>
```

#angular

<!-- prettier-ignore -->
```ts [icon-button.ts]
import { isPlatformBrowser } from "@angular/common";
import {
  Component,
  Directive,
  ElementRef,
  type OnChanges,
  type OnDestroy,
  type OnInit,
  PLATFORM_ID,
  Renderer2,
  inject,
  input,
  viewChild,
} from "@angular/core";

/** The scope attributes taken off each host, for the roots its component renders later. */
const scopes = new WeakMap<Element, string[]>();

/**
 * Gives a root node of its component the scope attributes the component's styled parents put on
 * its host (ADR-0063), and takes them off the host, which keeps only `display: contents`.
 */
@Directive({ selector: "[ufScope]" })
export class ScopeAttributes implements OnInit {
  readonly ufScope = input.required<HTMLElement>();
  private readonly element = inject<ElementRef<Element>>(ElementRef);
  private readonly renderer = inject(Renderer2);

  ngOnInit(): void {
    const host = this.ufScope();
    let names = scopes.get(host);
    if (!names) {
      names = host.getAttributeNames().filter((name) => name.startsWith("data-uf-"));
      for (const name of names) this.renderer.removeAttribute(host, name);
      scopes.set(host, names);
    }
    for (const name of names) this.renderer.setAttribute(this.element.nativeElement, name, "");
  }
}

/** A native listener of each event a consumer passes, by the event's name (ADR-0118). */
type Listeners = { [K in keyof HTMLElementEventMap]?: (event: HTMLElementEventMap[K]) => void };

/**
 * Puts what a consumer passes the component, and the component does not declare, on the element
 * that takes it (ADR-0118): the attributes, the consumer's winning over the element's own, and
 * the native listeners, which run after the element's own.
 */
@Directive({ selector: "[ufFallthrough]" })
export class Fallthrough implements OnChanges, OnDestroy {
  readonly ufFallthrough = input.required<{
    attributes: Record<string, unknown> | undefined;
    listeners: Listeners | undefined;
  }>();
  private readonly element = inject<ElementRef<Element>>(ElementRef);
  private readonly renderer = inject(Renderer2);
  private readonly browser = isPlatformBrowser(inject(PLATFORM_ID));
  private readonly listening = new Map<string, { handler: unknown; stop: () => void }>();
  private written: Record<string, unknown> = {};

  ngOnChanges(): void {
    const element = this.element.nativeElement;
    const { attributes = {}, listeners = {} } = this.ufFallthrough();
    for (const name of Object.keys(this.written)) {
      if (!(name in attributes)) this.renderer.removeAttribute(element, name);
    }
    for (const [name, value] of Object.entries(attributes)) {
      if (value === null || value === undefined) this.renderer.removeAttribute(element, name);
      else this.renderer.setAttribute(element, name, String(value));
    }
    this.written = attributes;
    if (!this.browser) return;
    const handlers = new Map(Object.entries(listeners));
    for (const [event, { handler, stop }] of this.listening) {
      if (handlers.get(event) === handler) continue;
      stop();
      this.listening.delete(event);
    }
    for (const [event, handler] of handlers) {
      if (handler === undefined || this.listening.has(event)) continue;
      this.listening.set(event, { handler, stop: this.renderer.listen(element, event, handler) });
    }
  }

  ngOnDestroy(): void {
    for (const { stop } of this.listening.values()) stop();
  }
}

@Component({
  selector: "uf-icon-button",
  imports: [ScopeAttributes, Fallthrough],
  host: { "[attr.class]": "null", "[attr.style]": "'display: contents;'" },
  preserveWhitespaces: false,
  template: `
    @let label = this.label();
    <button
      type="button"
      [attr.aria-label]="'aria-label' in (this.ufAttributes() ?? {}) ? this.ufAttributes()?.['aria-label'] : label"
      [class]="this.class()"
      [style]="this.style()"
      [ufFallthrough]="{ attributes: this.ufAttributes(), listeners: this.ufListeners() }"
      [ufScope]="this.host"
      #ufElement
    >
      <svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true">
        <path d="M8 1l2 5h5l-4 3 2 6-5-4-5 4 2-6-4-3h5z" />
      </svg>
    </button>
  `,
})
export default class IconButton implements OnInit {
  readonly label = input<string>();
  readonly class = input<string>();
  readonly style = input<Record<string, string | null | undefined>>();
  readonly ufAttributes = input<Record<string, unknown>>();
  readonly ufListeners = input<Listeners>();
  readonly ufElement = viewChild<ElementRef<HTMLButtonElement>>("ufElement");

  ngOnInit(): void {
    if (typeof ngDevMode === "undefined" || ngDevMode) {
      if (!this.label()) console.warn("IconButton has no accessible name: pass it a `label`.");
    }
  }
  protected readonly host = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement;
}
```

#solid

<!-- prettier-ignore -->
```tsx [IconButton.tsx]
import { type ComponentProps, type JSX, splitProps, untrack } from "solid-js";
import { isDev } from "solid-js/web";

export default function IconButton(
  props: { label?: string } & Omit<ComponentProps<"button">, "label" | "style"> & {
      style?: JSX.CSSProperties;
    },
) {
  const [, others] = splitProps(props, ["label"]);

  untrack(() => {
    if (isDev) {
      if (!props.label) console.warn("IconButton has no accessible name: pass it a `label`.");
    }
  });

  return (
    <button type="button" aria-label={props.label} {...others}>
      <svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true">
        <path d="M8 1l2 5h5l-4 3 2 6-5-4-5 4 2-6-4-3h5z" />
      </svg>
    </button>
  );
}
```

#qwik

<!-- prettier-ignore -->
```tsx [IconButton.tsx]
import {
  type CSSProperties,
  type ClassList,
  type PropsOf,
  component$,
  untrack,
} from "@qwik.dev/core";
import { isDev } from "@qwik.dev/core/build";

export default component$<
  { label?: string } & Omit<PropsOf<"button">, "label" | "class" | "style"> & {
      class?: ClassList;
      style?: CSSProperties;
    }
>(({ label, class: className, style, ...attributes }) => {
  untrack(() => {
    if (isDev) {
      if (!label) console.warn("IconButton has no accessible name: pass it a `label`.");
    }
  });

  return (
    <button type="button" aria-label={label} class={className} style={style} {...attributes}>
      <svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true">
        <path d="M8 1l2 5h5l-4 3 2 6-5-4-5 4 2-6-4-3h5z" />
      </svg>
    </button>
  );
});
```

#astro

<!-- prettier-ignore -->
```astro [IconButton.astro]
---
import type { HTMLAttributes } from "astro/types";

interface Props extends Omit<HTMLAttributes<"button">, "label"> {
  label?: string;
  class?: string;
  style?: Record<string, string | number | undefined>;
}

const { label, class: className, style, type, "aria-label": ariaLabel, ...attrs } = Astro.props;

if (import.meta.env.DEV) {
  if (!label) console.warn("IconButton has no accessible name: pass it a `label`.");
}
---

<button
  type={"type" in Astro.props ? type : "button"}
  aria-label={"aria-label" in Astro.props ? ariaLabel : label}
  class:list={[className]}
  style={style}
  {...attrs}
>
  <svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true">
    <path d="M8 1l2 5h5l-4 3 2 6-5-4-5 4 2-6-4-3h5z" />
  </svg>
</button>
```

::

React runs its body on every render, so its check runs on each one, and more than once under `StrictMode`, as React's own warnings do. The block writes nothing the component holds, so a development render and a production one show the same thing.

## A client check

Some checks need the rendered DOM, like the text a parent filled in. Write `if (isDev)` in client code, a lifecycle hook, a handler or a watcher's callback, and it runs there:

<!-- prettier-ignore -->
```tsx [IconLink.uf.tsx]
import { isDev, onMounted, useTemplateRef } from "@uxfront/unframework";

// A link whose content its parent fills. Only the rendered DOM can say whether it holds a name,
// so a development build checks it once mounted, in client code (ADR-0117).
export default function IconLink({ href, label }: { href: string; label?: string }) {
  const link = useTemplateRef<HTMLAnchorElement>();
  onMounted(() => {
    if (isDev) {
      const name = link.value?.getAttribute("aria-label") ?? link.value?.textContent?.trim();
      if (!name) console.warn("IconLink has no accessible name: give it a `label` or some text.");
    }
  });
  return (
    <a ref={link} href={href} aria-label={label}>
      <svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true">
        <path d="M2 8h10M8 4l4 4-4 4" />
      </svg>
    </a>
  );
}
```

Astro runs no client code, so a hook's check never runs there, and its output leaves the hook out ([UF4001](/docs/reference/diagnostics#UF4001)).

## What the compiler checks

`isDev` is read in one form: as the whole test of an `if` with no `else`. Anything else is [UF2040](/docs/reference/diagnostics#UF2040), since a development build and a production one could then render differently. That covers its value, a negation, an `else`, a template, a getter, a composable and a directive's hook, and a template, a getter or an initial value that calls a function which tests it.

A component that reads `isDev` declares nothing named `process` or `ngDevMode`, a prop, a constant or a parameter: the check React, Vue, Svelte and Angular write reads that global, which the name would hide ([UF2003](/docs/reference/diagnostics#UF2003)).

Inside a setup check, the setup's own rules hold. It may declare its own `const` and `let`, loop and branch, and call `console`, but it writes no state, emits no event, calls no setup function, reads no browser global but `console`, never returns, and reads only what the setup declares before it ([UF2023](/docs/reference/diagnostics#UF2023)). It may `throw`, to fail loudly in development where a production build renders on.

## What your app does

A production build needs nothing more: every framework's build replaces or resolves its check. A React, Vue or Svelte server reads `process.env.NODE_ENV` as it runs, as React and Vue themselves do, so start a production server with `NODE_ENV=production`, or its render warns too. A package built with `unframework build --package` keeps each check as written, so the app's build decides.

The decision behind this page is [ADR-0117](https://github.com/uxfront-com/unframework/blob/main/docs/adrs/0117-development-only-code-with-isdev.md).
