Giter Site home page Giter Site logo

Comments (6)

KTibow avatar KTibow commented on June 10, 2024 1

As SVGO caters to standalone SVGs, this isn't a bug, but SVGO working as intended - removing unused definitions. However you can customize SVGO to disable that behavior, by turning off removeHiddenElems and cleanupIds, as these both have the sole purpose to optimize defs within the SVG.

from svgo.

KTibow avatar KTibow commented on June 10, 2024

This issue is caused by either cleanupIds or removeHiddenElems

from svgo.

KTibow avatar KTibow commented on June 10, 2024

What is the original SVG's purpose? It seems to be to define things, as it renders nothing. How is it being used?

from svgo.

tomcru avatar tomcru commented on June 10, 2024

It is used as a Sprite sheet (instead of requesting a lot of icons on a page, we can just request this file once and pick the item out of it with <use>).

It is for all icons here:
https://main.gamegator.net/games/starfield
These, for example:
Screenshot 2023-12-21 at 16 12 48

Example on how it is used in a React component:

import { VariantProps, cva, cx } from 'class-variance-authority';
import { ComponentPropsWithoutRef } from 'react';
import { Sprite } from '../../../common/types/Sprite';

const styles = cva('', {
  variants: {
    color: {
      primary: 'text-icon-primary',
      secondary: 'text-icon-secondary',
      tertiary: 'text-icon-tertiary',
      success: 'text-icon-success',
      'on-color': 'text-icon-on-color',
      'on-color-inverse': 'text-icon-on-color-inverse',
      error: 'text-icon-error',
      none: '',
    },
  },
  defaultVariants: {
    color: 'none',
  },
});

type Props = {
  path: Sprite;
  className?: string;
  width?: number;
  height?: number;
  style?: Omit<ComponentPropsWithoutRef<'svg'>['style'], 'width' | 'height'>;
} & VariantProps<typeof styles>;

export function Icon({
  path,
  className,
  width,
  height,
  style,
  ...props
}: Props): JSX.Element {
  /** helper to find broken icons */
  if (!path) {
    return <div className="block h-24 w-24 bg-button-danger"></div>;
  }
  const [type, name] = path.split('/');

  return (
    <svg
      className={cx(styles(props), className)}
      style={{
        width: `${width}px`,
        height: `${height}px`,
        ...style,
      }}
    >
      <use href={`/sprites/${type}.svg#${name}`} />
    </svg>
  );
}

from svgo.

tomcru avatar tomcru commented on June 10, 2024

You can basically render the elements inside defs with a # and the id of it, like /sprites/regions.svg#LatinAmerica

from svgo.

tomcru avatar tomcru commented on June 10, 2024

Thank you, this was indeed the case - had to disable both removeHiddenElems & cleanupIds.

module.exports = {
  plugins: [
    {
      name: 'preset-default',
      params: {
        overrides: {
          removeHiddenElems: false,
          cleanupIds: false,
        },
      },
    },
  ],
};

from svgo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.