daybrush/guides

repository·master·Indexed 18 days ago

https://github.com/daybrush/guides

A Guides component for drawing rulers and managing guidelines. It provides a core library (@scena/guides) and framework-specific wrappers for React (@daybrush/react-guides), Angular (ngx-guides), Preact (@daybrush/preact-guides), and others. Key features include customizable ruler units (px, cm, in), snapping behavior, zoom control, and event-driven updates for guideline changes.

Tokens
6.7K
Snippets
30
Records
38
Agent score
62%

What's inside @scena/guides

  1. Configure Ruler Units and Zoom

    master

    The default unit is px with a line drawn every 50px. To use different units (like cm or in), you must adjust the zoom and unit properties.

    • zoom: The multiplier to convert your target unit to pixels.
    • unit: The interval at which a line is drawn in your target unit.

    Examples:

    • 1px (Default): zoom: 1, unit: 50 (every 50px)
    • 1cm: zoom: 37.7952, unit: 1 (every 1cm)
    • 1in: zoom: 96, unit: 1 (every 1in)
  2. Install @scena/guides via script tag

    master

    You can include the Guides component directly in your HTML using a script tag pointing to the latest distribution.

    <script src="//daybrush.com/guides/release/latest/dist/guides.min.js"></script>
  3. Use the Guides component

    master

    Initialize the Guides component by passing a container element (e.g., document.body) and an options object. You can listen to the changeGuides event to react to guideline changes. To ensure the ruler stays aligned during window resizing or scrolling, call .resize(), .scroll(), or .scrollGuides() manually.

    import Guides from "@scena/guides";
    
    const guides = new Guides(document.body, {
        type: "horizontal",
    }).on("changeGuides", e => {
        console.log(e.guides);
    });
    
    
    let scrollX = 0;
    let scrollY = 0;
    window.addEventListener("resize", () => {
        guides.resize();
    });
    
    window.addEventListener("wheel", e => {
        scrollX += e.deltaX;
        scrollY += e.deltaY;
    
        guides.scrollGuides(scrollY);
        guides.scroll(scrollX);
    });
  4. Configure Guides via constructor options

    master

    When instantiating Guides, you can pass a Partial<GuidesOptions> object.

    Key option:

    • warpSelf: (boolean) If true, the guides will render directly into the provided container instead of creating a new div wrapper. If false (default), a new div is appended to the container.
    const guides = new Guides(container, {
        warpSelf: true
    });
  5. Configure GuidesOptions for React Guides

    master

    The GuidesOptions interface defines the configuration for the Guides component. It extends RulerProps and includes settings for snapping, styling, and display behavior.

    Key Configuration Options:

    • className: CSS class name for the guides container. (Default: "")
    • rulerStyle: CSS style object for the ruler. (Default: { width: '100%', height: '100%' })
    • snapThreshold: The interval to snap to. (Default: 5)
    • snaps: An array of specific positions to snap to. (Default: [])
    • displayDragPos: Whether to show the moving position during dragging. (Default: false)
    • guidesZoom: Zoom level for guides (side zoom). If not set, it defaults to the ruler's zoom. (Default: zoom)
    • dragPosFormat: A function to format the drag position value. (Default: self)
    • defaultGuides: Initial array of guideline positions. (Default: [])
    • showGuides: Whether to render the guidelines. (Default: true)
    • lockGuides: Prevents adding, changing, or removing guides via interaction. Can be true or an array of specific actions: "add" | "change" | "remove". (Default: false)
    • digit: Number of decimal places for guidelines. (Default: 0)
    • guideStyle: CSS style object for guide elements. (Default: "{}")
    • displayGuidePos: Whether to show the guide position text. (Default: false)
    • scrollOptions: Configuration for automatic scrolling via DragScrollOptions. (Default: null)
    • guidesOffset: Numerical offset from the guideline position. (Default: 0)
  6. Configure Svelte builds with buildHelper

    master

    The Svelte guides package uses @daybrush/builder's buildHelper to generate multiple output formats (CJS and ESM) from a single configuration. The build process utilizes rollup-plugin-svelte with @pyoner/svelte-ts-preprocess for TypeScript support within Svelte components.

    Key configuration details:

    • Input: Defaults to ./src/index.js.
    • External Dependencies: svelte is marked as an external dependency.
    • Outputs:
      • CommonJS: dist/guides.cjs.js
      • ES Modules: dist/guides.esm.js
    import buildHelper from "@daybrush/builder";
    import svelte from 'rollup-plugin-svelte';
    import { preprocess } from "@pyoner/svelte-ts-preprocess";
    
    const defaultOptions = {
        tsconfig: "",
        input: './src/index.js',
        commonjs: true,
        external: {
            "svelte": "svelte",
        },
        plugins: [
            svelte({
                preprocess: preprocess(),
            }),
        ],
    }
    
    export default buildHelper([
        {
            ...defaultOptions,
            output: "dist/guides.cjs.js",
            format: "cjs",
        },
        {
            ...defaultOptions,
            output: "dist/guides.esm.js",
            format: "es",
        },
    ]);
  7. Import the Guides component from @daybrush/preact-guides

    master

    The @daybrush/preact-guides package provides a default export named Guides. You can import this component to integrate guides into your Preact application. It also re-exports type declarations from @scena/react-guides/declaration/types to ensure type safety when working with guide configurations and structures.

    import Guides from "@daybrush/preact-guides";