Components

Floating panel

A window over the page that its header drags and its edges resize, kept within the viewport, minimized, maximized, and stacked among others.

Previews are drawn in the theme you made.

Edit it
header
scheme
direction

Notes #

import { Bundle } from 'foldkit-bundle'
import { Bundles, Button, FloatingPanel, FloatingPanelSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Primary = Recipes.Button.style({})

// A window over the page: the header drags it, the edges and corners resize
// it, and the arrows do both from the keys. Where it is and how big is the
// Bundle's Model. `FloatingPanel.focusReturn` gives focus back to the button
// when it closes; it is opt-in, as it sends no Message.
const App = Bundle.compose({}).pipe(
  Bundle.withMessages({ Opened: {} }),
  Bundle.withChild('notes', Bundles.FloatingPanel, {
    args: { id: 'basic-notes', rect: { x: 80, y: 120, width: 320, height: 220 } },
    onOut: Bundle.ignore,
  }),
)

export default example({
  title: 'Notes',
  slots: FloatingPanelSlots,
  recipe: Recipes.FloatingPanel,
  state: App.placements.runtime({
    Model: App.Model,
    initial: {},
    update: App.placements.update(model =>
      App.children.notes.helpers.openBeside('basic-opener')(model),
    ),
  }),
  view: ({ style, model, onMessage, h }) =>
    h.div(
      [],
      [
        Button.view(
          {
            label: 'Open notes',
            onClick: onMessage(App.Message.Opened()),
            attributes: { button: [h.Id('basic-opener')] },
            style: Primary,
          },
          h,
        ),
        FloatingPanel.view(
          {
            model: model.notes,
            onMessage: message => onMessage(App.Message.GotNotesMessage({ message })),
            title: 'Notes',
            content: h.p([], ['Drag the header, resize from an edge, or use the arrows.']),
            style,
            mixins: FloatingPanel.focusReturn,
          },
          h,
        ),
      ],
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Beside its opener #

import { Bundle } from 'foldkit-bundle'
import { Bundles, Button, FloatingPanel, FloatingPanelSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Primary = Recipes.Button.style({})

// `openBeside` opens the panel next to the element with that id: a Command
// reads the opener's box after the update, and `Placed` puts the panel there,
// kept within the viewport. Its size stays the Model's.
const App = Bundle.compose({}).pipe(
  Bundle.withMessages({ Opened: {} }),
  Bundle.withChild('panel', Bundles.FloatingPanel, {
    args: { id: 'beside-panel', rect: { x: 0, y: 0, width: 280, height: 140 } },
    onOut: Bundle.ignore,
  }),
)

export default example({
  title: 'Beside its opener',
  slots: FloatingPanelSlots,
  recipe: Recipes.FloatingPanel,
  state: App.placements.runtime({
    Model: App.Model,
    initial: {},
    update: App.placements.update(model =>
      App.children.panel.helpers.openBeside('beside-opener', { side: 'bottom', gap: 8 })(model),
    ),
  }),
  view: ({ style, model, onMessage, h }) =>
    h.div(
      [],
      [
        Button.view(
          {
            label: 'Open below me',
            onClick: onMessage(App.Message.Opened()),
            attributes: { button: [h.Id('beside-opener')] },
            style: Primary,
          },
          h,
        ),
        FloatingPanel.view(
          {
            model: model.panel,
            onMessage: message => onMessage(App.Message.GotPanelMessage({ message })),
            title: 'Details',
            content: h.p([], ['Opened 8 pixels below the button, aligned to its start.']),
            style,
            mixins: FloatingPanel.focusReturn,
          },
          h,
        ),
      ],
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Docked to an edge #

import { Match, Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Button, FloatingPanel, FloatingPanelSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Primary = Recipes.Button.style({})
const Plain = Recipes.Button.style({ variant: 'outline', size: 'sm' })

// `helpers.dock` (the `Docked` Message) lays the panel along an edge of the
// viewport as measured when it lands, `thickness` across and the whole of the
// other side. It stays a window: drag it away and it floats again.
const App = Bundle.compose({}).pipe(
  Bundle.withMessages({ Opened: {}, DockedTo: { edge: Schema.Literals(['left', 'right']) } }),
  Bundle.withChild('panel', Bundles.FloatingPanel, {
    args: { id: 'docked-panel', rect: { x: 80, y: 120, width: 280, height: 180 } },
    onOut: Bundle.ignore,
  }),
)

export default example({
  title: 'Docked to an edge',
  slots: FloatingPanelSlots,
  recipe: Recipes.FloatingPanel,
  state: App.placements.runtime({
    Model: App.Model,
    initial: {},
    update: App.placements.update((model, message) =>
      Match.value(message).pipe(
        Match.tagsExhaustive({
          Opened: () => App.children.panel.helpers.openBeside('docked-opener')(model),
          DockedTo: ({ edge }) => App.children.panel.helpers.dock(edge, 300)(model),
        }),
      ),
    ),
  }),
  view: ({ style, model, onMessage, h }) => {
    const dock = (edge: 'left' | 'right', label: string) =>
      Button.view({ label, onClick: onMessage(App.Message.DockedTo({ edge })), style: Plain }, h)
    return h.div(
      [],
      [
        Button.view(
          {
            label: 'Open the inspector',
            onClick: onMessage(App.Message.Opened()),
            attributes: { button: [h.Id('docked-opener')] },
            style: Primary,
          },
          h,
        ),
        FloatingPanel.view(
          {
            model: model.panel,
            onMessage: message => onMessage(App.Message.GotPanelMessage({ message })),
            title: 'Inspector',
            content: h.div(
              [h.Style({ display: 'flex', gap: '0.5rem', flexWrap: 'wrap' })],
              [dock('left', 'Dock left'), dock('right', 'Dock right')],
            ),
            style,
            mixins: FloatingPanel.focusReturn,
          },
          h,
        ),
      ],
    )
  },
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

A stack of two #

import { Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Button, FloatingPanel, FloatingPanelSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Primary = Recipes.Button.style({})

// Several panels stack by `order`, the application's list of their ids from
// the bottom. A press or focus inside one reports `Raised`, which `onOut`
// folds with `FloatingPanel.raise`; the topmost is marked `data-topmost`.
const App = Bundle.compose({ order: Schema.Array(Schema.String) }).pipe(
  Bundle.withMessages({ Opened: {} }),
  Bundle.withChild('notes', Bundles.FloatingPanel, {
    args: { id: 'stack-notes', rect: { x: 40, y: 140, width: 260, height: 160 } },
    onOut: out => model => ({
      model: { ...model, order: FloatingPanel.raise(model.order, out.id) },
    }),
  }),
  Bundle.withChild('chat', Bundles.FloatingPanel, {
    args: { id: 'stack-chat', rect: { x: 170, y: 190, width: 260, height: 160 } },
    onOut: out => model => ({
      model: { ...model, order: FloatingPanel.raise(model.order, out.id) },
    }),
  }),
)

export default example({
  title: 'A stack of two',
  slots: FloatingPanelSlots,
  recipe: Recipes.FloatingPanel,
  state: App.placements.runtime({
    Model: App.Model,
    initial: { order: ['stack-notes', 'stack-chat'] },
    update: App.placements.update(model => {
      const notes = App.children.notes.helpers.openBeside('stack-opener')(model)
      const chat = App.children.chat.helpers.openBeside('stack-opener', { side: 'right' })(
        notes.model,
      )
      return { model: chat.model, commands: [...(notes.commands ?? []), ...(chat.commands ?? [])] }
    }),
  }),
  view: ({ style, model, onMessage, h }) =>
    h.div(
      [],
      [
        Button.view(
          {
            label: 'Open both',
            onClick: onMessage(App.Message.Opened()),
            attributes: { button: [h.Id('stack-opener')] },
            style: Primary,
          },
          h,
        ),
        FloatingPanel.view(
          {
            model: model.notes,
            onMessage: message => onMessage(App.Message.GotNotesMessage({ message })),
            title: 'Notes',
            content: h.p([], ['Press either panel to bring it to the front.']),
            order: model.order,
            style,
          },
          h,
        ),
        FloatingPanel.view(
          {
            model: model.chat,
            onMessage: message => onMessage(App.Message.GotChatMessage({ message })),
            title: 'Chat',
            content: h.p([], ['The one on top is marked data-topmost.']),
            order: model.order,
            style,
          },
          h,
        ),
      ],
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Controls and words #

import { Bundle } from 'foldkit-bundle'
import { Bundles, Button, FloatingPanel, FloatingPanelSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Primary = Recipes.Button.style({})

// `controls` names the buttons the header draws, and `words` their names:
// here only maximize and close, with the words of a different language.
// Restore appears with whichever stage change was made, so a panel that can
// be maximized can come back.
const App = Bundle.compose({}).pipe(
  Bundle.withMessages({ Opened: {} }),
  Bundle.withChild('panel', Bundles.FloatingPanel, {
    args: { id: 'controls-panel', rect: { x: 80, y: 120, width: 320, height: 180 } },
    onOut: Bundle.ignore,
  }),
)

export default example({
  title: 'Controls and words',
  slots: FloatingPanelSlots,
  recipe: Recipes.FloatingPanel,
  state: App.placements.runtime({
    Model: App.Model,
    initial: {},
    update: App.placements.update(model =>
      App.children.panel.helpers.openBeside('controls-opener')(model),
    ),
  }),
  view: ({ style, model, onMessage, h }) =>
    h.div(
      [],
      [
        Button.view(
          {
            label: 'Ouvrir',
            onClick: onMessage(App.Message.Opened()),
            attributes: { button: [h.Id('controls-opener')] },
            style: Primary,
          },
          h,
        ),
        FloatingPanel.view(
          {
            model: model.panel,
            onMessage: message => onMessage(App.Message.GotPanelMessage({ message })),
            title: 'Notes',
            content: h.p([], ['Agrandir, restaurer, fermer.']),
            controls: ['maximize', 'close'],
            words: {
              maximize: 'Agrandir',
              restore: 'Restaurer',
              close: 'Fermer',
              move: 'Déplacer {title}',
              resize: 'Redimensionner {title}',
            },
            style,
            mixins: FloatingPanel.focusReturn,
          },
          h,
        ),
      ],
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Fixed in place #

import { Bundle } from 'foldkit-bundle'
import { Bundles, Button, FloatingPanel, FloatingPanelSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Primary = Recipes.Button.style({})

// Not draggable, not resizable, and only a close button: a card that floats
// where it was put. Without the drag trigger and the corner the panel draws no
// tab stop of its own, so the arrows have nothing to move.
const App = Bundle.compose({}).pipe(
  Bundle.withMessages({ Opened: {} }),
  Bundle.withChild('tip', Bundles.FloatingPanel, {
    args: { id: 'fixed-tip', rect: { x: 80, y: 140, width: 280, height: 120 } },
    onOut: Bundle.ignore,
  }),
)

export default example({
  title: 'Fixed in place',
  slots: FloatingPanelSlots,
  recipe: Recipes.FloatingPanel,
  state: App.placements.runtime({
    Model: App.Model,
    initial: {},
    update: App.placements.update(model =>
      App.children.tip.helpers.openBeside('fixed-opener')(model),
    ),
  }),
  view: ({ style, model, onMessage, h }) =>
    h.div(
      [],
      [
        Button.view(
          {
            label: 'Show the tip',
            onClick: onMessage(App.Message.Opened()),
            attributes: { button: [h.Id('fixed-opener')] },
            style: Primary,
          },
          h,
        ),
        FloatingPanel.view(
          {
            model: model.tip,
            onMessage: message => onMessage(App.Message.GotTipMessage({ message })),
            title: 'Tip',
            content: h.p([], ['Press Escape or the button to put it away.']),
            draggable: false,
            resizable: false,
            controls: ['close'],
            style,
            mixins: FloatingPanel.focusReturn,
          },
          h,
        ),
      ],
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

A grid and size limits #

import { Bundle } from 'foldkit-bundle'
import { Bundles, Button, FloatingPanel, FloatingPanelSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Primary = Recipes.Button.style({})

// `grid` snaps every move and resize to multiples of 40 pixels, and `min` and
// `max` bound the size a resize reaches. They are the Bundle's args, kept in
// its Model, so a drag and the arrow keys read the one copy.
const App = Bundle.compose({}).pipe(
  Bundle.withMessages({ Opened: {} }),
  Bundle.withChild('panel', Bundles.FloatingPanel, {
    args: {
      id: 'limits-panel',
      rect: { x: 80, y: 120, width: 280, height: 200 },
      grid: 40,
      min: { width: 200, height: 120 },
      max: { width: 440, height: 320 },
    },
    onOut: Bundle.ignore,
  }),
)

export default example({
  title: 'A grid and size limits',
  slots: FloatingPanelSlots,
  recipe: Recipes.FloatingPanel,
  state: App.placements.runtime({
    Model: App.Model,
    initial: {},
    update: App.placements.update(model =>
      App.children.panel.helpers.openBeside('limits-opener')(model),
    ),
  }),
  view: ({ style, model, onMessage, h }) =>
    h.div(
      [],
      [
        Button.view(
          {
            label: 'Open the panel',
            onClick: onMessage(App.Message.Opened()),
            attributes: { button: [h.Id('limits-opener')] },
            style: Primary,
          },
          h,
        ),
        FloatingPanel.view(
          {
            model: model.panel,
            onMessage: message => onMessage(App.Message.GotPanelMessage({ message })),
            title: 'Snapped',
            content: h.p([], ['Moves and resizes land on a 40 pixel grid, 200 to 440 wide.']),
            style,
            mixins: FloatingPanel.focusReturn,
          },
          h,
        ),
      ],
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Past the edges #

import { Bundle } from 'foldkit-bundle'
import { Bundles, Button, FloatingPanel, FloatingPanelSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Primary = Recipes.Button.style({})

// `overflow: true` lets the panel be dragged past the edges of the viewport,
// as a palette that is parked half off screen is.
const App = Bundle.compose({}).pipe(
  Bundle.withMessages({ Opened: {} }),
  Bundle.withChild('panel', Bundles.FloatingPanel, {
    args: {
      id: 'overflow-panel',
      rect: { x: 80, y: 120, width: 280, height: 140 },
      overflow: true,
    },
    onOut: Bundle.ignore,
  }),
)

export default example({
  title: 'Past the edges',
  slots: FloatingPanelSlots,
  recipe: Recipes.FloatingPanel,
  state: App.placements.runtime({
    Model: App.Model,
    initial: {},
    update: App.placements.update(model =>
      App.children.panel.helpers.openBeside('overflow-opener')(model),
    ),
  }),
  view: ({ style, model, onMessage, h }) =>
    h.div(
      [],
      [
        Button.view(
          {
            label: 'Open the palette',
            onClick: onMessage(App.Message.Opened()),
            attributes: { button: [h.Id('overflow-opener')] },
            style: Primary,
          },
          h,
        ),
        FloatingPanel.view(
          {
            model: model.panel,
            onMessage: message => onMessage(App.Message.GotPanelMessage({ message })),
            title: 'Palette',
            content: h.p([], ['Drag it half way off the screen.']),
            style,
            mixins: FloatingPanel.focusReturn,
          },
          h,
        ),
      ],
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Configure once #

import { Bundle } from 'foldkit-bundle'
import { Bundles, Button, FloatingPanel, FloatingPanelSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'

const Primary = Recipes.Button.style({})

// What every window of an application starts from, said once: focus goes back
// to what opened it, and Escape is left to the page. A preset is a named set
// over it. The limits are the Model's (`init`), not a preset's.
const AppWindow = FloatingPanel.configure({
  base: { mixins: FloatingPanel.focusReturn, closeOnEscape: false },
  presets: { tool: { controls: ['close'], resizable: false } },
})

const App = Bundle.compose({}).pipe(
  Bundle.withMessages({ Opened: {} }),
  Bundle.withChild('panel', Bundles.FloatingPanel, {
    args: { id: 'configure-panel', rect: { x: 80, y: 120, width: 280, height: 140 } },
    onOut: Bundle.ignore,
  }),
)

export default example({
  title: 'Configure once',
  slots: FloatingPanelSlots,
  recipe: Recipes.FloatingPanel,
  state: App.placements.runtime({
    Model: App.Model,
    initial: {},
    update: App.placements.update(model =>
      App.children.panel.helpers.openBeside('configure-opener')(model),
    ),
  }),
  view: ({ style, model, onMessage, h }) =>
    h.div(
      [],
      [
        Button.view(
          {
            label: 'Open the tool',
            onClick: onMessage(App.Message.Opened()),
            attributes: { button: [h.Id('configure-opener')] },
            style: Primary,
          },
          h,
        ),
        AppWindow.view(
          {
            preset: 'tool',
            model: model.panel,
            onMessage: message => onMessage(App.Message.GotPanelMessage({ message })),
            title: 'Tool',
            content: h.p([], ['Closed by its button only.']),
            style,
          },
          h,
        ),
      ],
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Usage

Each example above is a module of its own, shown whole and run as shown, over the theme module Create a theme writes. The page hands it the style picked above.

The recipe ↗This code ↗

  • Keys of your own Add the application’s keys with the routes option, beside the ones the widget handles; the widget’s conflict(routes) judges a candidate set without drawing it.
  • Keeping focus when something is removed A control that leaves the page takes its focus with it: say where focus goes next, to a neighbour (FocusRecovery), to the opener (focusReturn) or to a control (keepFocus).