Interface Context

Represents the context of Penpot, providing access to various Penpot functionalities and data.

interface Context {
    root: null | Shape;
    currentFile: null | File;
    currentPage: null | Page;
    viewport: Viewport;
    history: HistoryContext;
    library: LibraryContext;
    fonts: FontsContext;
    currentUser: User;
    activeUsers: ActiveUser[];
    theme: Theme;
    selection: Shape[];
    shapesColors(shapes: Shape[]): (Color & ColorShapeInfo)[];
    replaceColor(shapes: Shape[], oldColor: Color, newColor: Color): void;
    uploadMediaUrl(name: string, url: string): Promise<ImageData>;
    uploadMediaData(name: string, data: Uint8Array, mimeType: string): Promise<ImageData>;
    group(shapes: Shape[]): null | Group;
    ungroup(group: Group, ...other: Group[]): void;
    createRectangle(): Rectangle;
    createBoard(): Board;
    createEllipse(): Ellipse;
    createPath(): Path;
    createBoolean(boolType: BooleanType, shapes: Shape[]): null | Boolean;
    createShapeFromSvg(svgString: string): null | Group;
    createText(text: string): null | Text;
    generateMarkup(shapes: Shape[], options?: {
        type?: "html" | "svg";
    }): string;
    generateStyle(shapes: Shape[], options?: {
        type?: "css";
        withPrelude?: boolean;
        includeChildren?: boolean;
    }): string;
    addListener<T>(type: T, callback: ((event: EventsMap[T]) => void), props?: {
        [key: string]: unknown;
    }): symbol;
    removeListener(listenerId: symbol): void;
    openViewer(): void;
    createPage(): Page;
    openPage(page: Page): void;
    alignHorizontal(shapes: Shape[], direction: "center" | "left" | "right"): void;
    alignVertical(shapes: Shape[], direction: "center" | "top" | "bottom"): void;
    distributeHorizontal(shapes: Shape[]): void;
    distributeVertical(shapes: Shape[]): void;
    flatten(shapes: Shape[]): Path[];
}

Properties

root: null | Shape

The root shape in the current Penpot context. Requires content:read permission.

const rootShape = context.root;
console.log(rootShape);
currentFile: null | File

Retrieves file data from the current Penpot context. Requires content:read permission.

Returns the file data or null if no file is available.

const fileData = context.currentFile;
console.log(fileData);
currentPage: null | Page

The current page in the Penpot context. Requires content:read permission.

const currentPage = context.currentPage;
console.log(currentPage);
viewport: Viewport

The viewport settings in the Penpot context.

const viewportSettings = context.viewport;
console.log(viewportSettings);

Context encapsulating the history operations

const historyContext = context.history;
console.log(historyContext);

The library context in the Penpot context, including both local and connected libraries. Requires library:read permission.

const libraryContext = context.library;
console.log(libraryContext);

The fonts context in the Penpot context, providing methods to manage fonts. Requires content:read permission.

const fontsContext = context.fonts;
console.log(fontsContext);
currentUser: User

The current user in the Penpot context. Requires user:read permission.

const currentUser = context.currentUser;
console.log(currentUser);
activeUsers: ActiveUser[]

An array of active users in the Penpot context. Requires user:read permission.

const activeUsers = context.activeUsers;
console.log(activeUsers);
theme: Theme

The current theme (light or dark) in Penpot.

const currentTheme = context.theme;
console.log(currentTheme);
selection: Shape[]

The currently selected shapes in Penpot. Requires content:read permission.

const selectedShapes = context.selection;
console.log(selectedShapes);

Methods

  • Retrieves colors applied to the given shapes in Penpot. Requires content:read permission.

    Parameters

    Returns (Color & ColorShapeInfo)[]

    Returns an array of colors and their shape information.

    const colors = context.shapesColors(shapes);
    console.log(colors);
  • Replaces a specified old color with a new color in the given shapes. Requires content:write permission.

    Parameters

    Returns void

    context.replaceColor(shapes, oldColor, newColor);
    
  • Uploads media to Penpot and retrieves its image data. Requires content:write permission.

    Parameters

    • name: string

      The name of the media.

    • url: string

      The URL of the media to be uploaded.

    Returns Promise<ImageData>

    Returns a promise that resolves to the image data of the uploaded media.

    const imageData = await context.uploadMediaUrl('example', 'https://example.com/image.jpg');
    console.log(imageData);

    // to insert the image in a shape we can do
    const board = penpot.createBoard();
    const shape = penpot.createRectangle();
    board.appendChild(shape);
    shape.fills = [{ fillOpacity: 1, fillImage: imageData }];
  • Uploads media to penpot and retrieves the image data. Requires content:write permission.

    Parameters

    • name: string

      The name of the media.

    • data: Uint8Array

      The image content data

    • mimeType: string

    Returns Promise<ImageData>

    Returns a promise that resolves to the image data of the uploaded media.

    const imageData = await context.uploadMediaData('example', imageData, 'image/jpeg');
    console.log(imageData);
  • Groups the specified shapes. Requires content:write permission.

    Parameters

    • shapes: Shape[]

      An array of shapes to group.

    Returns null | Group

    Returns the newly created group or null if the group could not be created.

    const penpotShapesArray = penpot.selection;
    penpot.group(penpotShapesArray);
  • Ungroups the specified group. Requires content:write permission.

    Parameters

    • group: Group

      The group to ungroup.

    • Rest...other: Group[]

      Additional groups to ungroup.

    Returns void

    const penpotShapesArray = penpot.selection;
    // We need to make sure that something is selected, and if the selected shape is a group,
    if (selected.length && penpot.utils.types.isGroup(penpotShapesArray[0])) {
    penpot.group(penpotShapesArray[0]);
    }
  • Use this method to create the shape of a rectangle. Requires content:write permission.

    Returns Rectangle

    const shape = penpot.createRectangle();
    // just change the values like this
    shape.name = "Example rectangle";

    // for solid color
    shape.fills = [{ fillColor: "#7EFFF5" }];
    // for linear gradient color
    shape.fills = [{
    fillColorGradient: {
    "type": "linear",
    "startX": 0.5,
    "startY": 0,
    "endX": 0.5,
    "endY": 1,
    "width": 1,
    "stops": [
    {
    "color": "#003ae9",
    "opacity": 1,
    "offset": 0
    },
    {
    "color": "#003ae9",
    "opacity": 0,
    "offset": 1
    }
    ]
    }
    }];
    // for a image fill
    const imageData = await context.uploadMediaUrl('example', 'https://example.com/image.jpg');
    shape.fills = [{ fillOpacity: 1, fillImage: imageData }];

    shape.borderRadius = 8;
    shape.strokes = [
    {
    strokeColor: "#2e3434",
    strokeStyle: "solid",
    strokeWidth: 2,
    strokeAlignment: "center",
    },
    ];
  • Use this method to create a board. This is the first step before anything else, the container. Requires content:write permission. Then you can add a gridlayout, flexlayout or add a shape inside the board. Just a heads-up: board is a board in Penpot UI.

    Returns Board

    const board = penpot.createBoard();

    // to add grid layout
    board.addGridLayout();
    // to add flex layout
    board.addFlexLayout();

    // to create a shape inside the board
    const shape = penpot.createRectangle();
    board.appendChild(shape);
  • Use this method to create the shape of a ellipse. Requires content:write permission.

    Returns Ellipse

    const shape = penpot.createEllipse();
    // just change the values like this
    shape.name = "Example ellipse";

    // for solid color
    shape.fills = [{ fillColor: "#7EFFF5" }];
    // for linear gradient color
    shape.fills = [{
    fillColorGradient: {
    "type": "linear",
    "startX": 0.5,
    "startY": 0,
    "endX": 0.5,
    "endY": 1,
    "width": 1,
    "stops": [
    {
    "color": "#003ae9",
    "opacity": 1,
    "offset": 0
    },
    {
    "color": "#003ae9",
    "opacity": 0,
    "offset": 1
    }
    ]
    }
    }];
    // for a image fill
    const imageData = await context.uploadMediaUrl('example', 'https://example.com/image.jpg');
    shape.fills = [{ fillOpacity: 1, fillImage: imageData }];

    shape.strokes = [
    {
    strokeColor: "#2e3434",
    strokeStyle: "solid",
    strokeWidth: 2,
    strokeAlignment: "center",
    },
    ];
  • Use this method to create a path. Requires content:write permission.

    Returns Path

    const path = penpot.createPath();
    path.name = "My path";

    // for solid color
    path.fills = [{ fillColor: "#7EFFF5" }];
  • Creates a Boolean shape based on the specified boolean operation and shapes. Requires content:write permission.

    Parameters

    • boolType: BooleanType

      The type of boolean operation ('union', 'difference', 'exclude', 'intersection').

    • shapes: Shape[]

      An array of shapes to perform the boolean operation on.

    Returns null | Boolean

    Returns the newly created Boolean shape resulting from the boolean operation.

    const booleanShape = context.createBoolean('union', [shape1, shape2]);
    
  • Creates a Group from an SVG string. Requires content:write permission.

    Parameters

    • svgString: string

      The SVG string representing the shapes to be converted into a group.

    Returns null | Group

    Returns the newly created Group containing the shapes from the SVG.

    const svgGroup = context.createShapeFromSvg('<svg>...</svg>');
    
  • Creates a Text shape with the specified text content. Requires content:write permission.

    Parameters

    • text: string

      The text content for the Text shape.

    Returns null | Text

    Returns the new created shape, if the shape wasn't created can return null.

    const board = penpot.createBoard();
    let text;
    text = penpot.createText();
    // just change the values like this
    text.growType = 'auto-height';
    text.fontFamily = 'Work Sans';
    text.fontSize = '12';
    text.fills = [{fillColor: '#9f05ff', fillOpacity: 1}];
    text.strokes = [{strokeOpacity: 1, strokeStyle: 'solid', strokeWidth: 2, strokeColor: '#deabff', strokeAlignment: 'outer'}];
    board.appendChild(text);
  • Generates markup for the given shapes. Requires content:read permission

    Parameters

    • shapes: Shape[]
    • Optionaloptions: {
          type?: "html" | "svg";
      }
      • Optionaltype?: "html" | "svg"

    Returns string

    const markup = context.generateMarkup(shapes, { type: 'html' });
    console.log(markup);
  • Generates styles for the given shapes. Requires content:read permission

    Parameters

    • shapes: Shape[]
    • Optionaloptions: {
          type?: "css";
          withPrelude?: boolean;
          includeChildren?: boolean;
      }
      • Optionaltype?: "css"
      • OptionalwithPrelude?: boolean
      • OptionalincludeChildren?: boolean

    Returns string

    const styles = context.generateStyle(shapes, { type: 'css' });
    console.log(styles);
  • Adds the current callback as an event listener

    Type Parameters

    Parameters

    • type: T
    • callback: ((event: EventsMap[T]) => void)
        • (event): void
        • Parameters

          Returns void

    • Optionalprops: {
          [key: string]: unknown;
      }
      • [key: string]: unknown

    Returns symbol

    const listenerId = context.addListener('selectionchange', (event) => {
    console.log(event);
    });
  • Removes the listenerId from the list of listeners

    Parameters

    • listenerId: symbol

    Returns void

    context.removeListener(listenerId);
    
  • Opens the viewer section. Requires content:read permission.

    Returns void

  • Changes the current open page to given page. Requires content:read permission.

    Parameters

    • page: Page

      the page to open

    Returns void

    context.openPage(page);
    
  • Aligning will move all the selected layers to a position relative to one of them in the horizontal direction.

    Parameters

    • shapes: Shape[]

      to align

    • direction: "center" | "left" | "right"

      where the shapes will be aligned

    Returns void

  • Aligning will move all the selected layers to a position relative to one of them in the vertical direction.

    Parameters

    • shapes: Shape[]

      to align

    • direction: "center" | "top" | "bottom"

      where the shapes will be aligned

    Returns void

  • Distributing objects to position them horizontally with equal distances between them.

    Parameters

    • shapes: Shape[]

      to distribute

    Returns void

  • Distributing objects to position them vertically with equal distances between them.

    Parameters

    • shapes: Shape[]

      to distribute

    Returns void

  • Converts the shapes into Paths. If the shapes are complex will put together all its paths into one.

    Parameters

    • shapes: Shape[]

      to flatten

    Returns Path[]