Skip to content

Commit 4de8d96

Browse files
authored
fix(svg/filter): add baseurl prop (danilowoz#144)
* fix(svg/filter): add baseurl prop * test(baseurl): add tests * docs(prop): baseurl
1 parent b8e5cb9 commit 4de8d96

File tree

7 files changed

+52
-11
lines changed

7 files changed

+52
-11
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ Defaults to `true`. Opt-out of animations with `false`
7474

7575
Defaults to `Loading interface...`. It's used to describe what element it is. Use `false` to remove.
7676

77+
**`baseUrl? string`**
78+
79+
Required if you're using `<base url="/" />` in your `<head/>`. Defaults to an empty string. This prop is commom used as: `<ContentLoader baseUrl={window.location.pathname} />` which will fill the svg attribute with the relative path. Related [#93](https://github.com/danilowoz/react-content-loader/issues/93).
80+
7781
**`speed?: number`**
7882

7983
Defaults to `2`. Animation speed in seconds.

docs/props.mdx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ Defaults to `Loading interface...`. It's used to describe what element it is. Us
2222
<Facebook ariaLabel="my custom loader" />
2323
</Playground>
2424

25+
## **`baseUrl? string`**
26+
27+
Required if you're using `<base url="/" />` in your `<head/>`. Defaults to an empty string. This prop is commom used as: `<ContentLoader baseUrl={window.location.pathname} />` which will fill the svg attribute with the relative path. Related [#93](https://github.com/danilowoz/react-content-loader/issues/93).
28+
29+
<Playground>
30+
<Facebook baseUrl="/mypage" />
31+
</Playground>
32+
2533
## `speed?: number`
2634

2735
Defaults to `2`. Animation speed in seconds.

src/Holder.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { IContentLoaderProps } from './interface'
66
export const defaultProps: IContentLoaderProps = {
77
animate: true,
88
ariaLabel: 'Loading interface...',
9+
baseUrl: '',
910
gradientRatio: 2,
1011
height: 130,
1112
interval: 0.25,

src/Svg.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export default ({
1010
style,
1111
width,
1212
height,
13+
baseUrl,
1314
gradientRatio,
1415
animate,
1516
ariaLabel,
@@ -45,8 +46,8 @@ export default ({
4546
y="0"
4647
width={width}
4748
height={height}
48-
clipPath={`url(#${idClip})`}
49-
style={{ fill: `url(#${idGradient})` }}
49+
clipPath={`url(${baseUrl}#${idClip})`}
50+
style={{ fill: `url(${baseUrl}#${idGradient})` }}
5051
/>
5152

5253
<defs>

src/__tests__/Holder.tests.tsx

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,22 @@ describe('Holder', () => {
3030
const withPropsComponent = ShallowRenderer.createRenderer()
3131
withPropsComponent.render(
3232
<ContentLoader
33-
rtl
34-
speed={10}
35-
interval={0.5}
36-
width={200}
33+
animate={false}
34+
ariaLabel="My custom loading title"
35+
baseUrl="/mypage"
36+
className="random-className"
3737
gradientRatio={0.5}
3838
height={200}
39-
animate={false}
39+
interval={0.5}
40+
preserveAspectRatio="xMaxYMax meet"
4041
primaryColor="#000"
41-
secondaryColor="#fff"
4242
primaryOpacity={0.06}
43+
rtl
44+
secondaryColor="#fff"
4345
secondaryOpacity={0.12}
44-
preserveAspectRatio="xMaxYMax meet"
45-
className="random-className"
46+
speed={10}
4647
style={{ marginBottom: '10px' }}
47-
ariaLabel="My custom loading title"
48+
width={200}
4849
/>
4950
)
5051

@@ -174,5 +175,14 @@ describe('Holder', () => {
174175
expect(typeof propsFromFullfield.ariaLabel).toBe('string')
175176
expect(propsFromFullfield.ariaLabel).toBe('My custom loading title')
176177
})
178+
179+
it("`baseUrl` is a string and it's used", () => {
180+
// defaultProps
181+
expect(typeof propsFromEmpty.baseUrl).toBe('string')
182+
expect(propsFromEmpty.baseUrl).toBe('')
183+
// custom props
184+
expect(typeof propsFromFullfield.baseUrl).toBe('string')
185+
expect(propsFromFullfield.baseUrl).toBe('/mypage')
186+
})
177187
})
178188
})

src/__tests__/Svg.tests.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ describe('Svg', () => {
2323
title: wrapper.findByType('title'),
2424
}
2525

26+
it('baseUrl is used correctly', () => {
27+
const baseUrl = '/page-path'
28+
const wrapperWithBaseUrl = renderer.create(<Svg baseUrl={baseUrl} />).root
29+
30+
const clipPath = wrapperWithBaseUrl.findByType('clipPath')
31+
const linearGradient = wrapperWithBaseUrl.findByType('linearGradient')
32+
const rectClipPath = wrapperWithBaseUrl.find(predicateRectClipPath)
33+
34+
expect(rectClipPath.props.clipPath).toBe(
35+
`url(${baseUrl}#${clipPath.props.id})`
36+
)
37+
expect(rectClipPath.props.style.fill).toBe(
38+
`url(${baseUrl}#${linearGradient.props.id})`
39+
)
40+
})
41+
2642
describe('it has basic elements necessary to work ', () => {
2743
it('has a `rect` with `clipPath`', () => {
2844
const { allRectClipPath } = partsOfComponent

src/interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export interface IContentLoaderProps {
22
animate?: boolean
33
ariaLabel?: string | boolean
44
children?: React.ReactNode
5+
baseUrl?: string
56
className?: string
67
height?: number
78
preserveAspectRatio?:

0 commit comments

Comments
 (0)