1.0.0 • Published 4 months ago

@svg-use/core v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

@svg-use/core

Core utilities for transforming an SVG to make it compatible with use[href] (for example ensuring an id, extracting id and viewBox, making it themeable).

Also includes utilities for creating JS modules, to facilitate bundler plugins or more bespoke code generation.

If you want to integrate svg-use into an existing pipeline, look into the webpack loader or the rollup plugin.

If you want to transform SVG for a reusable component library (instead of an application), or as a codegen step (instead of bundler plugins), then consult the reusable library example

Quick start

pnpm add --save-dev @svg-use/core

Here are the basic transformations, assuming some SVG content in scope:

import {
  transformSvgForUseHref,
  createJsModule,
  defaultGetSvgIdAttribute,
  defaultComponentFactory,
  getDefaultThemeSubstitutionFunction,
} from '@svg-use/core';

function transformAndWriteModule(content: string) {
  const transformResult = transformSvgForUseHref(content, {
    getSvgIdAttribute: defaultGetSvgIdAttribute,
    getThemeSubstitutions: getDefaultThemeSubstitutionFunction(),
  });

  if (transformResult.type === 'failure') {
    throw new Error(transformResult.error);
  }

  const {
    data: { content: transformedSvg, id, viewBox },
  } = transformResult;

  const jsModuleCode = createJsModule(
    {
      url: /* fill in an applicable URL here; depends on the context */,
      id: JSON.stringify(id),
      viewBox: JSON.stringify(viewBox),
    },
    {
      componentFactory: defaultComponentFactory,
    },
  );
}

API reference

Find the full API reference

1.0.0

4 months ago

0.5.0

4 months ago

0.4.0

11 months ago

0.3.0

11 months ago

0.2.0

11 months ago

0.1.1

11 months ago

0.1.0

11 months ago