Returning a Dynamic Styled Component - styled-components

I'm trying to return this element but if I try to style it after importing it, it wont apply the correct styles.
Heading.js:
export default ({
element = 'h1',
}) => new styled[element]`
margin: 0 0 ${shevy[element].marginBottom};
font-family: Plain, sans-serif;
font-size: ${shevy[element].fontSize};
line-height: ${shevy[element].lineHeight};
text-transform: uppercase;
`()
SomeOtherComponent.js:
const LeadHeading = styled(Heading)`
margin: 0 0 ${bs(3)};
font-size: ${h4.fontSize};
line-height: ${h4.lineHeight};
color: red;
`
<LeadHeading>Yup</LeadHeading>
The color is applied correctly but font-size, line-height, and margin aren't applied correctly.

I was doing something hacky, instead I should've been using withComponent: https://www.styled-components.com/docs/basics#extending-styles.
Ended up doing:
import styled from 'styled-components'
import shevy from 'common/shared/utils/shevy'
const Heading = styled.h1`
font-family: Plain, sans-serif;
text-transform: uppercase;
`
const extend = tag => `
margin: 0 0 ${() => shevy[tag].marginBottom};
font-size: ${() => shevy[tag].fontSize};
line-height: ${() => shevy[tag].lineHeight};
`
Heading.h1 = Heading
Heading.h2 = Heading.withComponent('h2').extend(extend('h2'))
Heading.h3 = Heading.withComponent('h3').extend(extend('h3'))
Heading.h4 = Heading.withComponent('h4').extend(extend('h4'))
Heading.h5 = Heading.withComponent('h5').extend(extend('h5'))
Heading.h6 = Heading.withComponent('h6').extend(extend('h6'))
export default Heading

Related

How to set initial value with styled component props

I am making app using styled components.
I would like to pass some props to position my element
In a situation where no props gets passed I want to set a value initial
Of course if I do something like in example I get error
Cannot find name 'initial'.ts(2304)
<SomeStyledComponent top="20px" />
const SomeStyledComponent = styled.div`
position: absolute;
top: ${(props) => props.top || initial} !important;
`
const SomeStyledComponent = styled.div`
position: absolute;
top: ${(props) => props.top ? props.top : initial} !important;
`
You get error because variable with name "initial" has not been defined.
You can define initial value direct in component.
In fourth variant , i show how fix error in your example.
// first variant
const Component = styled.div`
position: absolute;
top: ${(p) => `${p.top || 20}px`};
`
// second variant
const Component = styled.div`
position: absolute;
top: ${(p) => `${p.top}px`};
`
Component.defaultProps = {
top: 200
}
// third variant, set default value in wrapper component
const BaseComponent = styled.div`
position: absolute;
top: ${(p) => `${p.top}px`};
`;
const Component = ({ top = 50, children, ...rest }) => (
<BaseComponent top={top} {...rest}>
{children}
</BaseComponent>
);
// fourth variant, your example with fix
const initial = "200px"
const SomeStyledComponent = styled.div`
position: absolute;
top: ${(props) => props.top || initial} !important;
`

Styled components and scoping

I'm starting to work with styled-components and had a question about scoping.
This is just a dummy example but one that shows the point.
So I have a component. I setup a styled div called Wrapper then instead of creating another styled component to handle group, I thought be easier to just add a class to the inner div called .group and using nesting like in SCSS to style the inner div. This works but the problem with using className for the inner div is there could be a collision with global styles called .group
So, is there a way to avoid this with scoping somehow, or would I have to create another styled component called Group to handle that inner CSS ? Seems like a lot of boilerplate to have to add another styled component just to style the inner components.
const Wrapper = styled.div`
color: blue;
.group {
padding: 10px;
color: green;
}
`
const MyComponent = () => {
return (
<Wrapper>
<div className='group'>
<h1>heading text</h1>
<h2>subheading text</h2>
</div>
<div>This is my blue text</div>
</Wrapper>
);
}
Here is my globalStylesheet with group. Obviously this only has one style but it could have way more to handle grouped elements globally.
export default createGlobalStyle`
h1, h2, h3, h4, h5, h6 {
font-family: '.....';
}
.group {
background-color: red;
}
`;
I know I could also do
> div {
border: 1px solid red;
}
but I want to be able to be more explicit with a className
I think it's better to create another styled-component for group like
const Group = styled.div`
padding: 10px;
color: green;
`
So you can be sure that overwriting styles properly. And if there will be more styles in Wrapper, it stays less readable. Also you can easily replace Group component into children or make as parent(in this case you should rewrite .group style from Wrapper to another one).
In future to prevent boilerplate code you can rewrite existed styled-components like
const Timer = styled.div`
background: #ff5f36;
border-radius: 50%;
width: 48px;
height: 48px;
display: flex;
justify-content: center;
align-items: center;
font-family: GTWalsheim;
font-size: 32px;
color: #ffffff;
`
const TimeIsUp = styled(Timer)`
width: 172px;
border-radius: 8px;
`
EDIT
Also you can easily replace Group component into children or make as parent
I'll try to explain in code below
const MyComponent = () => {
return (
<Wrapper>
<div className='someClass'>
<Group> // so you can replace only your component, without rewriting any style
<h1>heading text</h1>
<h2>subheading text</h2>
</Group>
</div>
<div>This is my blue text</div>
</Wrapper>
);
}
I mean you can easily replace Group component to any place of code. While when you write style from parent as it was in Wrapper, you should replace this .group style from Wrapper to another element which is parent for .group

what are the changes in HTTP request format while migrating from Dialogflow v1 to Dialogflow v2

The below python code is what I am using as a http request to query dialogflow-V1.I want to migrate to v2.But I am facing issues when I changed parameters with respect to v2 naming.
headers = {
'Authorization': 'Bearer CLIENT_ACCESS_TOKEN'
}
params = (
('v', '20150910'),
('lang', 'en'),
('query', 'query_str'),
('sessionId', "10000"), #UNIQUE ID
('timezone', 'America/New_York'),
)
response = requests.get('https://api.dialogflow.com/v1/query', headers=headers, params=params)
Help me migrate the above DFv1 code to DFv2 format
below is what I tried
headers = {
'Authorization': 'Bearer #Client access token'
}
params = (
('queryInput.text.languageCode ', 'en'),
('detectIntent', 'query_str'),
('session', 'projects/your-project-id/agent/sessions/session-id'),
('queryParams.timeZone ', 'America/New_York'),
)
response = requests.get('https://api.dialogflow.com/v2/query', headers=headers, params=params)
I am not sure of what exactly is session id in DFv2.The response here is not is any format as expected.Below is the response
b'Dialogflow#-moz-keyframes blink {0%{opacity:1;} 50%{opacity:0;} 100%{opacity:1;}}#-webkit-keyframes blink {0%{opacity:1;} 50%{opacity:0;} 100%{opacity:1;}}#-ms-keyframes blink {0%{opacity:1;} 50%{opacity:0;} 100%{opacity:1;}}#keyframes blink {0%{opacity:1;} 50%{opacity:0;} 100%{opacity:1;}}#-moz-keyframes fadein {from{opacity:0;} to{opacity:1;}}#-webkit-keyframes fadein{from{opacity:0;} to{opacity:1;}}#-ms-keyframes fadein {from{opacity:0;} to{opacity:1;}}#keyframes fadein {from{opacity:0;} to{opacity:1;}}#loading-screen{background: #fff; position: fixed; top: 0; left: 0; height: 100%; width: 100%; z-index: 999999; opacity: 1; filter: alpha(opacity=100); -webkit-transition: opacity 500ms ease; transition: opacity 500ms ease;}#loading-screen #logo {display: block; width: 109px; height: 39px; background-repeat: no-repeat; background-image: url(\'https://www.gstatic.com/dialogflow-console/common/assets/img/logo#2x-black.png\'); background-size: contain; position: absolute; top: 50%; left: 50%; margin: -20px 0 0 -55px; -moz-transition:all 1s ease-in-out; -webkit-transition:all 1s ease-in-out; -o-transition:all 1s ease-in-out; -ms-transition:all 1s ease-in-out; transition:all 1s ease-in-out; -moz-animation:blink normal 2s infinite ease-in-out; -webkit-animation:blink normal 2s infinite ease-in-out; -ms-animation:blink normal 2s infinite ease-in-out; animation:blink normal 2s infinite ease-in-out;}#loading-screen #assistant-preview {width: 400px; height: 200px; position: absolute; top: 50%; left: 50%; margin: -120px 0 0 -200px; text-align: center; background: white; -webkit-animation: fadein 500ms; -moz-animation: fadein 500ms; -ms-animation: fadein 500ms; animation: fadein 500ms;}#loading-screen #assistant-preview .logo {width: 46px; height: 46px; background-repeat: no-repeat; background-image: url(\'https://www.gstatic.com/dialogflow-console/common/assets/img/logo_icon_48dp.png\'); background-size: contain; position: absolute; left:45%;}#loading-screen #assistant-preview .title {margin-top: 100px; font-size: 23px;}#loading-screen #assistant-preview .progress-container {margin: 25px 50px;}#loading-screen #assistant-preview .progress-container md-progress-linear, #loading-screen #assistant-preview .progress-container md-progress-linear .md-container, #loading-screen #assistant-preview .progress-container md-progress-linear .md-bar, #loading-screen #assistant-preview .progress-container md-progress-linear .md-dashed {height: 2px;}window.INTERNALIZED = true; window.DF_FLAGS = {email: "", backend: "https:\\/\\/api.dialogflow.com", opBackend: "https:\\/\\/dialogflow.clients6.google.com", apiKey: "AIzaSyD1dO8oRagJkmtkSJ9oLI289jIT8M4Zk5s",}; window.saveAs = undefined; window.i18n = undefined; window.d3 = undefined; window.addStyleString = function(str){var node = document.createElement(\'style\'); node.innerHTML = str; document.head.appendChild(node);};window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);}gtag(\'js\', new Date()); gtag(\'config\', \'UA-98266305-2\'); gtag(\'config\', \'UA-98266305-8\');Updating Actions on Google...var loadDeferredStyles = function() {var addStylesNode = document.getElementById("deferred-styles"); var replacement = document.createElement("div"); replacement.innerHTML = addStylesNode.textContent; document.body.appendChild(replacement); addStylesNode.parentElement.removeChild(addStylesNode);}; var raf = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame; if (raf) {raf(function() { window.setTimeout(loadDeferredStyles, 0); });} else {window.addEventListener(\'load\', loadDeferredStyles);}if (window.location.hash.endsWith(\'assistant_preview\')) {setTimeout(function () {document.getElementById(\'assistant-preview\').style.display = \'block\';}, 200);} else {document.getElementById(\'logo\').style.display = \'block\';}window.WIZ_global_data = {"nQyAE":{"EtBwpb":"false","He6Wuc":"false","xJW1Ve":"true","tm22M":"false","aVzR2d":"false","ox9Ggd":"false","Dk1LPb":"false","CXZ8Fd":"false","c9LSR":"true","tvMknd":"true","eiw7Vc":"false","tLPClf":"false","lAsnAb":"false","buULxf":"false","adjIR":"false"}};var AF_initDataKeys = []\n; var AF_dataServiceRequests = {}; var AF_initDataChunkQueue = []; var AF_initDataCallback; var AF_initDataInitializeCallback; if (AF_initDataInitializeCallback) {AF_initDataInitializeCallback(AF_initDataKeys, AF_initDataChunkQueue, AF_dataServiceRequests);}if (!AF_initDataCallback) {AF_initDataCallback = function(chunk) {AF_initDataChunkQueue.push(chunk);};}'
Please help me resolve this.
Thanks
The move from v1 to v2 is not that straightforward. There are a number of things you need to address. The "query" API call has changed to the detectIntent API call. This has a lot of consequences.
The first is that the URL path has dramatically changed. Consult the reference page linked above, but it needs to be something more like
https://dialogflow.googleapis.com/v2/projects/PROJECT_ID/agent/sessions/SESSION_ID:detectIntent
It also needs to be an HTTPS POST request - you can't use GET anymore.
Moving to POST also means that the request body is more complicated. While v1 had most of the parameters in the top level of a JSON structure, v2 has these in nested JSON objects. So although the migration guide says that "timezone" is replaced with "queryParams.timeZone", this really means that your JSON response would need to have a structure including
{
...
"queryParams": {
...
"timeZone": "America/NewYork"
...
}
...
}
As you note in the comments, the biggest change is how you need to do auth. Instead of using a single shared secret, as you did with v1, you need to get a Google Cloud Service Account and use the secret from that account to generate an access token, which usually has a limited time to live. The good news is that the Google Cloud libraries or SDK will generate (and sometimes manage) the access token for you.

Target another component on hover using emotion-js

I understand this is very similar to Target another styled component on hover
However I would like to achieve the same effect with emotion-js
More specifically I am trying to recreate this example using emotion styled components
Here is my code and what I have tried.
import React from 'react';
import styled from '#emotion/styled';
import { Link } from 'gatsby';
const Dropdown = styled.div`
postition: relative;
display: inline-block;
`;
const Button = styled.div`
background-color: green;
color: white;
&:hover {
${DropDownContent} {
display: block;
}
}
`;
const DropDownContent = styled.div`
display: none;
position: absolute;
`;
const DropDownMenu = () => {
return (
<Dropdown>
<Button>Dropdown</Button>
<DropDownContent>
<Link to="/">Link 1</Link>
</DropDownContent>
</Dropdown>
);
};
export default DropDownMenu;
I would like the link to show up when I hover the button, but that is not working and I cannot figure out why
There are three issues here.
You're referencing DropdownContent before you've defined it. Rearrange your code so that the DropdownContent declaration comes before the tagged template literals that use it and you should be good to go.
The resulting css selector (something like button:hover .DropDownContent) does not match your HTML document, where DropDownContent is a sibling of Button.
Your position property on Dropdown is misspelled.
With all three issues resolved, your code may look something like this:
import React from 'react';
import styled from '#emotion/styled';
import { Link } from 'gatsby';
const Dropdown = styled.div`
position: relative;
display: inline-block;
`;
const DropDownContent = styled.div`
display: none;
position: absolute;
`;
const Button = styled.div`
background-color: green;
color: white;
&:hover + ${DropDownContent} {
display: block;
}
`;
const DropDownMenu = () => {
return (
<Dropdown>
<Button>Dropdown</Button>
<DropDownContent>
<Link to="/">Link 1</Link>
</DropDownContent>
</Dropdown>
);
};
export default DropDownMenu;
as #coreyward mentioned, rearrange the code.... and the
import styled from "#emotion/styled/macro";
and this will do the trick
This solution was already posted at Component selectors can only be used in conjunction with babel-plugin-emotion error while using emotion by https://stackoverflow.com/users/165215/ijk

gnome shell extension stylesheet not loading

i'm trying to develop a gnome shell extension and I've created the "Hello World" extension which is created automatically with gnome-shell-extension-tool --create-extension
It creates 3 files: example.js, metadata.json, stylesheet.css.
I reload gnome-shell and the extension works correcty. The problem is, the styling file is not working at all; here is the code:
// Sample extension code, makes clicking on the panel show a message
const St = imports.gi.St;
const Mainloop = imports.mainloop;
const Main = imports.ui.main;
function _showHello() {
let text = new St.Label({ style_class: 'hello', text: "Hello, world!" });
let monitor = global.get_primary_monitor();
global.stage.add_actor(text);
text.set_position(Math.floor (monitor.width / 2 - text.width / 2), Math.floor(monitor.height / 2 - text.height / 2));
text.add_style_class_name("hello");
Mainloop.timeout_add(6000, function () { text.destroy(); });
}
// Put your extension initialization code here
function main() {
Main.panel.actor.reactive = true;
Main.panel.actor.connect('button-release-event', _showHello);
}
And here stylesheet.css:
/* Example stylesheet */
.hello {
font-size: 360px;
font-weight: bold;
color: #ffffff;
background-color: rgba(10,10,10,0.7);
border-radius: 5px;
}
I didn't know how to even get more information about this....any idea?
Try the following stylesheet:
.hello {
font-size: 36px;
font-weight: bold;
color: #ffffff;
background-color: rgba(10,10,10,0.7);
border-radius: 15px;
margin: 50px;
padding: 50px;
}
I've finally worked it out :D
The problem was that I had installed the "user-theme" extension wich creates conficts with the css management
Hope can help someone
P.S. I'm using ArchLinux 32bit version

Resources