-
-
Notifications
You must be signed in to change notification settings - Fork 424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
update(titleProp:true): title should fallback to svg's title #311
Conversation
… is not provided and titleProps is set to true
This pull request is automatically deployed with Now. |
Codecov Report
@@ Coverage Diff @@
## master #311 +/- ##
==========================================
+ Coverage 86.38% 86.58% +0.19%
==========================================
Files 30 30
Lines 470 477 +7
Branches 132 135 +3
==========================================
+ Hits 406 413 +7
Misses 53 53
Partials 11 11
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #311 +/- ##
==========================================
+ Coverage 86.38% 86.58% +0.19%
==========================================
Files 30 30
Lines 470 477 +7
Branches 132 135 +3
==========================================
+ Hits 406 413 +7
Misses 53 53
Partials 11 11
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One change, title
is actually define, so you can check it directly using title === undefined
instead of doing a typeof
comparison. Except that everything looks OK to me!
Thank you @neoziro for having a look into it. I'm kind of noobie to the babel plugin world. So I went to the babel docs to find a definition for creating this expression but failed. I don't know how create this expression this: {type === undefined ? "Default Title" : title} Please suggest the related docs. |
You can use https://astexplorer.net/ to see what type are used, then try to find the correct method in babel types. For example: t.binaryExpression(
'===',
expression,
t.identifier('undefined'),
) It should do the trick ;) |
On it. Thank you. |
@neoziro Updated. Please have a look. |
It looks good to me, I will merge it after the tests have ran. |
@neoziro That was quick. Thank you. Just one question: Will you be able to release it any time soon ? |
Thank you. |
@neoziro I was testing it in an application bootstrapped with CRA and got some issues. const existingTitle = (existingTitleChildren || [])
.map(c => c.value)
.join(); So the value is not accessible in the case of So I visited https://astexplorer.net/ and found that we need to handle these cases too. const existingTitle = (existingTitleChildren || [])
.map(c => {
switch (c.type) {
case 'JSXExpressionContainer':
return c.expression.value;
default:
return c.value;
}
})
.join(); Please help me out. What all cases can be there ? Or is there any better way to get the inner text as string. |
I think there are several cases: <title>Hello</title>
<title>{'Hello'}</title>
<title></title>
<title />
/* nothing */ Please add them to tests! |
Alright. I will add them and update. |
What if we do this <svg>
{title === undefined ? <title>{any existing children}</title> : <title>{title}</title>}
</svg> Then we do not need to handle any case separately. |
Summary
Fixes #310:
title
should fallback to SVG's title when titleProps is set to true and no title is providedI have updated the
@svgr/babel-plugin-svg-dynamic-title
plugin to insert a conditional statement for rendering title of the SVG. So now, if thetitleProp
is set to true and the SVG has a title element, then the React component will have following conditional statement to render the titleIf there is no title in the SVG, then there will be no change in the React component (same is existing).
Test plan
I have update the relevant test case and docs for the implemented functionality.
P.S.: This is my first attempt with a babel plugin's source code :). So please bear with me.