Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 10 additions & 32 deletions src/components/inlineSVG.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,22 @@
import React from 'react';
import React, { useRef, useEffect } from 'react';
import PropTypes from 'prop-types';

const getIframeStyle = (src, fitToSize) => {
let style = { border: 'none', height: '100%', width: '100%' };
const InlineSVG = ({ src, ...otherProps }) => {
const containerRef = useRef(null);

if (!fitToSize) return style;
useEffect(() => {
if (!containerRef.current || !src) return;

const parser = new DOMParser();
const doc = parser.parseFromString(src, 'image/svg+xml');
const svg = doc.querySelector('svg');
containerRef.current.innerHTML = '';
const shadow = containerRef.current.attachShadow({ mode: 'open' });
shadow.innerHTML = src;
}, [src]);

if (!svg) return style;

const width = svg.getAttribute('width') || (svg.viewBox?.baseVal?.width ?? null);
const height = svg.getAttribute('height') || (svg.viewBox?.baseVal?.height ?? null);

if (!width || !height) return style;

style = { ...style, width: `${width}px`, height: `${height}px` };
return style;
};

const InlineSVG = ({ src, fitToSize = false, ...otherProps }) => {
const iframeStyle = getIframeStyle(src, fitToSize);
return (
<div
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{ __html: src }}
{...otherProps}
/>
);
return <div ref={containerRef} {...otherProps} />;
};

InlineSVG.propTypes = {
src: PropTypes.string.isRequired,
fitToSize: PropTypes.bool,
};

InlineSVG.defaultProps = {
fitToSize: false,
};

export default InlineSVG;
1 change: 0 additions & 1 deletion src/components/map/stopMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ const LocationSymbol = props => (

const getSalesPointIcon = type => (
<InlineSVG
fitToSize
src={type.toLowerCase() === 'myyntipiste' ? ticketSalesPointIcon : ticketMachineIcon}
/>
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/stopPoster/adContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class AdContainer extends Component {
this.root = ref;
}}>
{ads.slice(0, this.state.spaces).map((src, i) => (
<InlineSVG key={i} style={iconStyle} src={src} fitToSize />
<InlineSVG key={i} style={iconStyle} src={src} />
))}
</div>
);
Expand Down
14 changes: 6 additions & 8 deletions src/components/stopPoster/stopPoster.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,7 @@ class StopPoster extends Component {
}

if (this.state.hasColumnTimetable) {
this.setState({
hasColumnTimetable: false,
});
return;
}

if (this.state.hasRoutes) {
this.setState({ hasRoutes: false });
this.setState({ hasColumnTimetable: false });
return;
}

Expand Down Expand Up @@ -276,6 +269,11 @@ class StopPoster extends Component {
return;
}

if (this.state.hasRoutes) {
this.setState({ hasRoutes: false });
return;
}

this.onError('Failed to remove layout overflow');
return;
}
Expand Down