Skip to content

v4.0.0

Choose a tag to compare

@graphieros graphieros released this 18 Nov 07:02
· 4 commits to main since this release

Grid Plan v4

Separate menu and inventory components

useGridPlan() connects the grid, menu and inventory through a single reactive controller.

<script setup>
import { ref } from "vue";
import {
  GridPlan,
  GridPlanMenu,
  GridPlanInventory,
  useGridPlan,
} from "grid-plan";

const placedItems = ref([
  { id: "server-1", typeId: 1, x: 1, y: 2, w: 2, h: 1 },
]);

const availableTypes = ref([
  { typeId: 1, description: "Server", color: "#6385ed", icon: "server" },
]);

const config = ref({ showGrid2d: true, showGrid3d: false });

const readonly = ref(false);

const plan = useGridPlan({ placedItems, availableTypes, config, readonly });
</script>

<template>
  <aside>
    <GridPlanMenu :plan="plan" title="Equipment" :accordion="false" />
  </aside>
  <GridPlan :plan="plan" />
  <footer>
    <GridPlanInventory :plan="plan" title="Placed equipment" />
  </footer>
</template>

Inputs accept plain values, refs, or getters. Use refs or getters for live updates.
Create separate controllers for independent plans. Existing direct props and component-ref methods still work.

Menu and inventory slots

Both controls provide default content and scoped slots.
Custom menu content handles its own clicks; legacy menu slots still select automatically.

<GridPlanMenu :plan="plan">
  <template #default="{ availableType, isActive, selectType }">
    <button
      :disabled="plan.readonly"
      :aria-pressed="isActive"
      @click="selectType"
    >
      {{ availableType.description }}
    </button>
  </template>
</GridPlanMenu>

<GridPlanInventory :plan="plan">
  <template
    #default="{ item, displayCoordinates, focusItem, deleteItem, isFocused }"
  >
    <span>{{ item.description }} · {{ displayCoordinates.label }}</span>
    <button :disabled="plan.readonly" @click="focusItem">
      {{ isFocused ? "Unfocus" : "Focus" }}
    </button>
    <button :disabled="plan.readonly" @click="deleteItem">Delete</button>
  </template>
</GridPlanInventory>

Hovering an inventory row highlights its item in the grids. Both controls accept title and accordion props.

Controller methods and events

Selection, hover hints, creation, deletion and events are available on plan.

plan.setActiveType(plan.availableTypes[0]);
plan.createItem({ x: 5, y: 5 });

const item = plan.getItems()[0];
plan.focusItem(item); // Toggle selection.
plan.hintItem(item); // Show hover feedback without selecting.
plan.hintItem(null); // Clear the hint.
plan.unselect();

const stop = plan.on("change", (item) => saveItem(item));
stop(); // Unsubscribe when needed.

Events include created, change, delete, select, unselect, selectType.
GridPlan also forwards them as component events. Choose one subscription path to avoid duplicate handling.

Creation rejects overlapping, out-of-bounds and non-integer positions without changing state or emitting events.
Incoming item and type updates refresh the shared state. Items without IDs receive generated IDs; supply IDs for persisted records.

3D editing

The 3D view supports creation, movement, resizing, depth changes, deletion. Both views share items and selection.
Drag empty 3D space to orbit and use the wheel to zoom.

Install Three.js only if you use 3D:

pnpm add grid-plan three
config.value = {
  ...config.value,
  showGrid2d: true,
  showGrid3d: true,
  grid3dPosition: "top", // Or "bottom".
  showBox: true,
  boxHeight: 1,
  boxThickness: 0.3,
  useShadow: true,
};

Movement snaps to cells, resizing respects grid bounds and edits prevent overlaps.
The selected item's Depth field controls its height. Escape cancels an ongoing drag.

Items can also have textures:

placedItems.value = placedItems.value.map((item) => ({
  ...item,
  depth: 2,
  texture: {
    src: "/textures/metal.png",
    repeatX: 2,
    repeatY: 2,
    rotation: 0,
    metalness: 0.7,
    roughness: 0.8,
  },
}));

Tooltips

Tooltips work in both views, including readonly mode. They follow edits and camera movement, flip near edges and stay inside the map.

config.value = {
  ...config.value,
  showTooltip: true,
  tooltipPosition: "right",
  tooltipBackgroundColor: "#202622f2",
  tooltipColor: "#ffffff",
  tooltipBorderRadius: 6,
  tooltipPadding: "10px 14px",
  tooltipWidth: "fit-content",
  tooltipMaxWidth: 320,
  tooltipMaxHeight: 400,
};

Borders, shadows and line height are also configurable. Size values accept pixels or CSS lengths.
Use the tooltip slot for custom content:

<GridPlan :plan="plan">
  <template #tooltip="{ item, coordinates, isSelected, view }">
    <strong>{{ item.description }}</strong>
    <div>{{ coordinates }} · {{ item.w }} × {{ item.h }}</div>
    <small>{{ isSelected ? "Selected" : "Hovered" }} in {{ view }}</small>
  </template>
</GridPlan>

Tooltips are on by default. Disable them to use inline item labels instead.

Item icons

Item icons accept 116 built-in names, SVG path data, or content from the componentIcon slot.
Icon size is fixed at one grid unit in both views, regardless of item dimensions.

availableTypes.value = [
  { typeId: 1, description: "Server", icon: "server", iconColor: "#ffffff" },
  {
    typeId: 2,
    description: "Custom equipment",
    icon: {
      paths: ["M4 4H28V28H4Z"],
      viewBox: "0 0 32 32",
      fill: "currentColor",
      stroke: "none",
    },
  },
];

Raw path strings and arrays of paths are also supported. For icon fonts or Vue components, omit the type's icon and use the slot:

<GridPlan :plan="plan">
  <template #componentIcon="{ maxSize, iconColor }">
    <i
      class="bi bi-server"
      aria-hidden="true"
      :style="{ fontSize: `${maxSize * 0.7}px`, color: iconColor }"
    />
  </template>
</GridPlan>

Load the icon library's CSS separately. HTML icons follow the 3D camera but are not occluded by other meshes; use SVG paths when depth occlusion matters.
Set showIcons: false to hide item icons in both views.

Coordinate labels and conversion

item.x and item.y stay zero-based numbers. Axis settings control the labels shown to users.
Inventory and tooltip slots also expose displayCoordinates; before and after slots expose a formatter with the same name.

plan.displayCoordinates(plan.items[0]);
// Default axes, x: 1, y: 2, w: 2, h: 1:
// { x: "B", y: "3", label: "B-3, C-3" }

Convert explicitly when a backend expects labels:

import { formatCoordinate, parseCoordinate } from "grid-plan";

formatCoordinate(1, "alphabetic"); // "B"
parseCoordinate("B", "alphabetic"); // 1
formatCoordinate(2, "numeric"); // "3"
parseCoordinate("3", "numeric"); // 2

Conversion helpers reject invalid inputs. Display settings do not change item positions.

Themes, labels, hover feedback

The theme option accepts "dark" and "light". Individual colors can be overridden.

config.value = {
  ...config.value,
  theme: "light",
  gridHighlightColor: "#0d9488",
  crosshairBackground: "#ccfbf1",
  itemNameFontSizeRatio: 0.5,
  itemCoordinatesFontSizeRatio: 0.4,
  itemNameOffsetY: 0,
  itemCoordinatesOffsetY: 0,
};

Hovering a 2D cell highlights its row, column and matching edge coordinates.
The coordinate cells share the crosshair fill. Hovered items have corner markers.
Labels, grid lines, handles, gradients and 3D walls remain configurable.

For a complete theme-aware config, use resolveConfig({ theme: "light" }) rather than spreading the dark defaultConfig.

Custom delete controls

The floating delete control stays near the selected item and adjusts at map edges.
Replace its icon in either view:

<GridPlan :plan="plan">
  <template #deleteIcon>
    <span style="display: grid; place-items: center; width: 100%; height: 100%">
      ×
    </span>
  </template>
</GridPlan>

The button keeps its accessible label, keyboard action and readonly behavior.

Keyboard navigation and readonly mode

Tab into either grid. Arrow keys move through items, Enter selects, Escape clears focus and selection.
Readonly mode keeps keyboard browsing, hover information, camera navigation available while blocking edits and selection.

readonly.value = true;

Disable action buttons in custom slots when readonly mode is active. Controller methods also enforce readonly mode.

TypeScript

Use the exported types for your items, configuration, controller and custom slots.

import type {
  GridPlanController,
  GridPlanExpose,
  GridPlanConfig,
  GridPlanResolvedConfig,
  GridPlanItem,
  GridPlanPlacedItem,
  GridPlanTooltipSlotProps,
  GridPlanMenuSlotProps,
  GridPlanInventorySlotProps,
} from "grid-plan";

Migrating from v3

Existing direct props, component-ref methods and slots remain available.
The #availableType and #inventory slots use the standalone controls internally; omit those slots when rendering the controls elsewhere.

Removed grid config Replacement
showMenu, showInventory Omit the legacy slot, or use v-if on an external control
accordionMenuTitle, inventoryTitle The control's title prop
useAccordionMenu GridPlanMenu's accordion prop
  • Without the legacy control slots, GridPlan renders no internal menu or inventory.
  • focusItem(item) now emits select when selecting and unselect when toggled off. Check handlers for duplicate work.
  • Readonly mode also blocks deletion and type selection through controller methods.
  • Defaults are 2D on, 3D off, tooltips on, dark theme.

For full API details, see the README.

The docs website and playground is up to date