Skip to content

Commit e3a2d57

Browse files
authored
AB#67030 fix svgs not resizing correctly in the ad panel (#505)
* AB#67030 fix svgs not resizing correctly in the ad panel * try with stateless approach using parser to get width and height before render
1 parent 66bb9dd commit e3a2d57

File tree

14 files changed

+67
-37
lines changed

14 files changed

+67
-37
lines changed

src/components/a3Timetable/a3TableHeader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33
import classNames from 'classnames';
4-
import { InlineSVG } from 'components/util';
4+
import InlineSVG from 'components/inlineSVG';
55
import timeLogo from '../../icons/time.svg';
66

77
import styles from './a3TableHeader.css';

src/components/a3stopPoster/a3header.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { Component } from 'react';
22
import PropTypes from 'prop-types';
3-
import { chunk, cloneDeep, sortBy } from 'lodash';
4-
import { Row, Column, InlineSVG } from 'components/util';
3+
import { chunk, sortBy } from 'lodash';
4+
import { Row, Column } from 'components/util';
55
import a3headerContainer from './a3headerContainer';
66
import renderQueue from 'util/renderQueue';
77
import { getColor } from 'util/domain';

src/components/inlineSVG.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
4+
const getIframeStyle = (src, fitToSize) => {
5+
let style = { border: 'none', height: '100%', width: '100%' };
6+
7+
if (!fitToSize) return style;
8+
9+
const parser = new DOMParser();
10+
const doc = parser.parseFromString(src, 'image/svg+xml');
11+
const svg = doc.querySelector('svg');
12+
13+
if (!svg) return style;
14+
15+
const width = svg.getAttribute('width') || (svg.viewBox?.baseVal?.width ?? null);
16+
const height = svg.getAttribute('height') || (svg.viewBox?.baseVal?.height ?? null);
17+
18+
if (!width || !height) return style;
19+
20+
style = { ...style, width: `${width}px`, height: `${height}px` };
21+
return style;
22+
};
23+
24+
const InlineSVG = ({ src, fitToSize = false, ...otherProps }) => {
25+
const iframeStyle = getIframeStyle(src, fitToSize);
26+
return (
27+
<div
28+
// eslint-disable-next-line react/no-danger
29+
dangerouslySetInnerHTML={{ __html: src }}
30+
{...otherProps}
31+
/>
32+
);
33+
};
34+
35+
InlineSVG.propTypes = {
36+
src: PropTypes.string.isRequired,
37+
fitToSize: PropTypes.bool,
38+
};
39+
40+
InlineSVG.defaultProps = {
41+
fitToSize: false,
42+
};
43+
44+
export default InlineSVG;

src/components/map/customMap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
33
import get from 'lodash/get';
44
import Measure from 'react-measure';
55
import StopMap from './stopMapContainer';
6-
import { InlineSVG } from '../util';
6+
import InlineSVG from 'components/inlineSVG';
77
import renderQueue from '../../util/renderQueue';
88
import { sizedSvg } from '../../util/sizedSvg';
99

src/components/map/stopMap.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import find from 'lodash/find';
55
import ItemContainer from 'components/labelPlacement/itemContainer';
66
import ItemFixed from 'components/labelPlacement/itemFixed';
77
import ItemPositioned from 'components/labelPlacement/itemPositioned';
8-
import { Row, InlineSVG } from 'components/util';
8+
import { Row } from 'components/util';
9+
import InlineSVG from 'components/inlineSVG';
910

1011
import locationIcon from 'icons/marker.svg';
1112
import subwayStationIcon from 'icons/icon-subway-station.svg';
@@ -50,6 +51,7 @@ const LocationSymbol = props => (
5051

5152
const getSalesPointIcon = type => (
5253
<InlineSVG
54+
fitToSize
5355
src={type.toLowerCase() === 'myyntipiste' ? ticketSalesPointIcon : ticketMachineIcon}
5456
/>
5557
);
@@ -73,7 +75,7 @@ const getZoneIcon = zone => {
7375
}
7476
};
7577

76-
const ZoneLabel = props => (
78+
const ZoneLabel = () => (
7779
<div className={styles.zoneHeading}>
7880
<span>
7981
<strong>Vyöhyke</strong> Zon/Zone

src/components/routeDiagram/routeDiagram.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565

6666
.start .icon {
6767
width: 30px;
68+
height: 36px;
6869
margin-left: -14px;
6970
margin-right: 5px;
7071
}

src/components/routeDiagram/routeDiagram.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import classNames from 'classnames';
22
import React from 'react';
33
import PropTypes from 'prop-types';
44

5-
import { InlineSVG } from 'components/util';
5+
import InlineSVG from 'components/inlineSVG';
66
import markerIcon from 'icons/marker.svg';
77

88
import Path from './path';

src/components/routeDiagram/stop.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import PropTypes from 'prop-types';
33
import classNames from 'classnames';
44

55
import { iconsByMode, trimRouteId, routeGeneralizer, getColor } from 'util/domain';
6-
import { Column, InlineSVG } from 'components/util';
6+
import { Column } from 'components/util';
7+
import InlineSVG from 'components/inlineSVG';
78
import styles from './stop.css';
89

910
const metroRegexp = / ?\(M\)$/;
@@ -45,7 +46,7 @@ class Stop extends Component {
4546
this.props.transferModes.forEach(mode => modes.add(mode));
4647

4748
const terminalAreaRouteList = this.getTerminalAreaRoutes(this.props);
48-
const terminalAreaRoutes = terminalAreaRouteList.routes.map((route, index) => {
49+
const terminalAreaRoutes = terminalAreaRouteList.routes.map(route => {
4950
return {
5051
text: route.text,
5152
trunkRoute: terminalAreaRouteList.trunkRoutes.includes(route.text),

src/components/stopPoster/adContainer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { Component } from 'react';
22
import PropTypes from 'prop-types';
33
import get from 'lodash/get';
4-
import { InlineSVG } from 'components/util';
4+
import InlineSVG from 'components/inlineSVG';
55
import renderQueue from 'util/renderQueue';
66
import styles from './stopPoster.css';
77

@@ -66,7 +66,7 @@ class AdContainer extends Component {
6666
this.root = ref;
6767
}}>
6868
{ads.slice(0, this.state.spaces).map((src, i) => (
69-
<InlineSVG key={i} style={iconStyle} src={src} />
69+
<InlineSVG key={i} style={iconStyle} src={src} fitToSize />
7070
))}
7171
</div>
7272
);

src/components/stopPoster/footer.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import React from 'react';
44
import PropTypes from 'prop-types';
55
import QrCode from 'components/qrCode';
6-
import { InlineSVG } from 'components/util';
6+
import InlineSVG from 'components/inlineSVG';
77
import classnames from 'classnames';
88
import get from 'lodash/get';
99
import cheerio from 'cheerio';
@@ -25,7 +25,7 @@ function getSvgElementPosition($element, widthModifier = 0, heightModifier = 0)
2525
const height = isLine
2626
? $element.attr('stroke-width')
2727
? // The stroke-width can be defined either as an attribute or as a style.
28-
parseAttr($element.attr('stroke-width'))
28+
parseAttr($element.attr('stroke-width'))
2929
: parseAttr($element.css('stroke-width'))
3030
: parseAttr($element.attr('height'));
3131

@@ -45,7 +45,7 @@ function getDynamicAreas(svg, widthModifier, heightModifier) {
4545
const dynamicAreas = $('.dynamic-area');
4646
const areas = [];
4747

48-
dynamicAreas.each((idx, element) => {
48+
dynamicAreas.each((_, element) => {
4949
const area = $(element);
5050
const areaType = area.data('area-type');
5151
const areaData = area.data('area-data');

0 commit comments

Comments
 (0)