Search box in react-leaflet looks wrong - reactjs

It looks like that when it is supposed to be a button that you press to open.
I am using react-leaflet with mapzen's leaflet-geocoder.
import { MapControl } from 'react-leaflet'
import L from 'leaflet'
import 'leaflet-geocoder-mapzen'
export default class SearchBox extends MapControl {
componentWillMount() {
const searchBox = L.control.geocoder(<API-KEY>)
this.leafletElement = searchBox
}
}

I added some styling in my CSS that made the search box look somewhat better — that's my fix for now, at least.
.leaflet-touch .leaflet-bar {
background-color: white;
}
.leaflet-pelias-control, .leaflet-pelias-input {
width: 100%;
max-width: 200px;
}
.leaflet-pelias-search-icon {
display: block;
visibility: hidden;
}
.leaflet-pelias-close {
visibility: hidden;
}
.leaflet-pelias-close::after {
content:'Clear search';
visibility: visible;
display: block;
width: 100%;
padding: 5px;
top: 2px;
}
.leaflet-top, .leaflet-left {
width: 200px;
}

For leaflet-control-geocoder, what worked for me, was to add the following to index.css:
#import "~leaflet-control-geocoder/dist/Control.Geocoder.css";
So for leaflet-geocoder-mapzen I'm guessing (untested):
#import "~leaflet-geocoder-mapzen/1.9.4/leaflet-geocoder-mapzen.css";
or similar
FYI
Tilde (~) used in conjunction with webpack means that a lookup is performed against node_modules to resolve the path.
see stackoverflow explanation here

Related

How to refer to individual style rules from a "*.module.scss" file in another .scss file?

I have this .button rule in Cta.module.scss-
.button {
$bg-color: white;
$text-color: variables.$color-text-cta;
background-color: $bg-color;
color: $text-color;
display: inline-block;
padding: variables.$padding-block-sm variables.$padding-inline-sm;
border: 0.056rem solid transparent;
border-radius: 0.278rem;
box-shadow: rgba(0, 0, 0, 0.239) 0 0.111rem 0.556rem;
#include mixins.cta-hover($bg-color, $text-color);
}
I want to override some properties for this .button class in IntroSection/index.module.scss, which I could do pretty easily if I was using styled-components; something like-
import {Button} from 'Cta/Cta.styles.js';
const Section = styled.section`
${Button} {
...
}
`;
I have no idea how (or if) we can achieve such a thing using scss modules. I tried a similar approach in index.module.scss-
#use '../../comps/Cta/Cta.module';
.section {
Cta.button {
padding: variables.$padding-block-md variables.$padding-inline-md !important;
&:first-of-type {
margin-right: variables.$margin-sm;
}
}
But it doesn't seem to work. What's the correct approach to achieve the result I want?
Below is what Cta.jsx contains in it-
import React from 'react';
import styles from './Cta.module.scss';
export default function Cta({ children, backgroundColor }) {
const classForBgColor = styles['button--' + backgroundColor];
return (
<button className={`${styles.button} ${classForBgColor || ''}`}>
{children}
</button>
);
}

Why my SASS project is disregarding media breakpoints?

I used to use CSS for a React project, but for the implementation of a "dark mode" functionality I decided to move to SASS.
I also use bootstrap so I organize my files according to the documentation.
/* App.scss */
#import "~bootstrap/scss/bootstrap.scss";
#import './styles/_themes';
#import './styles/_fonts';
h1, h2, h3, h4, h5, h6 {
font-family: $font-family-secondary;
#include themed() {
color: t($title-color)
}
}
p {
font-family: $font-family-primary;
#include themed() {
color: t($text-color)
}
}
hr, .nav-tabs {
#include themed() {
border-color: t($text-secondary-color)
}
}
.base {
height: 100%;
width: 100%;
#include themed() {
background-color: t($background-color)
}
}
.main {
width: 750px;
#include themed() {
background-color: t($background-color)
}
}
First, I thought it was a bootstrap problem, but the breakpoints are also being disregarding in my own files.
/* Footer.scss */
#media (max-width: 767px){
#footer h5 {
padding-left: 0;
border-left: transparent;
padding-bottom: 0px;
margin-bottom: 10px;
}
}
I made a minimal reproducible example and posted it to codesandbox: https://codesandbox.io/s/immutable-fire-qu9oe
Please, try to decrease the screen size and check the media breakpoints being disregarded.
What am I doing wrong?
Thank you very much!
EDIT:
Thanks to robertp's answer I could fix my custom media breakpoints.
But how can I fix bootstrap ones?
For example, in my navbar I have a media breakpoint for a bootstrap class:
/* Navbar.scss */
#media (max-width: 571px) {
.collapsing {
#include themed() {
position: absolute !important;
z-index: 3;
width: 100%;
top: 75px;
}
}
.collapse.show {
#include themed() {
display: block;
position: absolute;
z-index: 3;
width: 100%;
top: 75px;
}
}
.navbar.open {
#include themed() {
transform: translate(0, 0)
}
}
.overlay.open {
#include themed() {
height: 100%;
opacity: 1;
}
}
}
But it didn't work. Also, how I could fix all bootstrap grid system classes?
EDIT 2:
The problem was the import order. If I put the bootstrap import at the end of the file everythings works as expected!
It seems to be due to specificity in the theme. Your footer H5 element is styled as per the theme, so you need to override the themed version of it.
Screenshot of the themed styling overriding your mediaquery based styling:
This should work:
#media (max-width: 767px){
#footer h5 {
#include themed() {
padding-left: 0;
border-left: transparent;
padding-bottom: 0px;
margin-bottom: 10px;
}
}
}

How can I get variables from my Theme with styled components and use within astroturf css

I've come across a "problem" which is making me feel like I've chosen the wrong stack for what I'd like. I'm pretty new with React & Typescript but I wondered if anyone knew of a way too do the following.
theme.ts
import { createGlobalStyle } from "styled-components"
import { ThemeType } from "../types/theme"
export const lightTheme = {
background: "#F2F3EF",
text: "#252524",
}
export const darkTheme = {
background: "#1C282B",
text: "#F2F3EF",
}
export const GlobalStyles = createGlobalStyle<{ theme: ThemeType }>`
body {
margin: 0;
margin-left: 300px;
background: ${({ theme }) => theme.background};
color: ${({ theme }) => theme.text};
font-family: Tahoma, Helvetica, Arial, Roboto, sans-serif;
}
`
styles.ts
import { css } from "astroturf"
import { useTheme } from "styled-components"
const theme = useTheme()
const styles = css`
.navContainer {
width: 300px;
height: 100%;
position: absolute;
top: 0;
left: 0;
}
.themeToggle {
position: absolute;
bottom: 0;
text-align: left;
padding: 15px;
}
.logo {
padding: 20px;
}
.menuItems {
a {
text-decoration: none;
display: block;
padding: 5px;
}
a.light:hover {
background-color: ${({ theme }) => theme.background};
}
a.dark:hover {
background-color: ${({ theme }) => theme.background};
}
}
`
export default styles
I get the following error message, which DOES make sense
Could not resolve interpolation to a value, css returned class name,
or styled component. All interpolated styled components
must be in the same file and values must be statically determinable at
compile time
I'm running webpack with postcss but also, trying to use themes from style-components.
The end goal is to use a toggle switch to change theme from light/dark but I just need some guidance on what approach I should be taking. Clearly implementing css-in-js with astroturf for postcss is going to be problematic with the way I've used themes but I wanted to ask you guys before I tear it out.
There could be an easy solution which I'm missing. I have tried just importing the variables light and dark and then trying to use those within my css string but, this also doesnt work.
Thanks in advance

How do you style nested css in react?

I have nested selectors in my css file. I'm new to React and trying to make it work with my js file. Does anyone have any tips?
I found: https://blog.logrocket.com/the-best-styling-in-react-tutorial-youve-ever-seen-676f1284b945
It seems to be helpful but does not mention how to deal with nested css. For example, I have &:before and &:after, #include signUpActive, among others. This is really some confusing stuff.
.img {
overflow: hidden;
z-index: 2;
position: absolute;
&:before {
content: '';
position: absolute;
transition: transform 1.2s ease-in-out;
}
&:after {
content: '';
position: absolute;
}
#include signUpActive {
&:before {
transform: translate3d(640px,0,0);
}
}
&__text {
z-index: 2;
position: absolute;
transition: transform 1.2s ease-in-out;
&.m--up {
#include signUpActive {
transform: translateX(260px*2);
}
}
&.m--in {
transform: translateX(260px * -2);
#include signUpActive {
transform: translateX(0);
}
}
}
&__btn {
overflow: hidden;
z-index: 2;
position: relative;
&:after {
content: '';
z-index: 2;
position: absolute;
}
&.m--in {
transform: translateY(36px*-2);
#include signUpActive {
transform: translateY(0);
}
}
&.m--up {
#include signUpActive {
transform: translateY(36px*2);
}
}
}
}
}
It is actually not "nested CSS". But it is a SASS/SCSS code. Both SASS and SCSS have similar purposes, with several slight differences. Please check here for more detail: https://www.geeksforgeeks.org/what-is-the-difference-between-scss-and-sass/ . Personally, I would suggest SCSS over SASS by considering it is less strict than SASS.
If you like to use just normal CSS, then just write and import CSS scripts to your react. It will be no problem. You should not follow that "nested CSS" way which is SASS/SCSS ( I would not recommend that actually ).
Let assume you stick with SASS/SCSS. If you are using "CRA" ( create react application ), it is just as simple as import your .scss file to react component like the following:
import React from "react";
import ReactDOM from "react-dom";
import "./styles.scss";
function App() {
return (
<div className="App">
<div className="panel">
<h2>Panel</h2>
<div className="panel__inner">
<h3>Inner panel</h3>
</div>
</div>
</div>
);
}
While the styles.scss is just a normal SCSS code at same directory, like follow:
#import "./scss-mixins/mixins.scss"; //Using relative path to the .scss file
.App {
font-family: sans-serif;
text-align: center;
.panel {
padding: 15px;
background: #f9f9f9;
&__inner {
padding: 10px;
background: green;
}
}
}
Just ensure that you have node-sass in your package. Otherwise you should do npm install node-sass --save. Please read more at official documentation here: https://facebook.github.io/create-react-app/docs/adding-a-sass-stylesheet.
You can check working example at: https://codesandbox.io/s/x2z4ly6y2z

Styled components on hover change img src attribute

I have a styled component as below. It is a google social login button imgGoogleLogin is a path loaded by webpack.
I want to change the src attribute to another src when it is on hover.
Is there any way to achieve this? Thanks a lot
const GoogleLoginButton = styled.img.attrs({
src: imgGoogleLogin,
})`
width: 190px;
height: 45px;
&:hover {
cursor: pointer;
}
`;
I wanted to achieve something similar but I found that styled components cannot explicitly do this. I had to do it this way, ie create two components one hidden and when the parent is hovered I unhide it and hide the other one. Seems hacky but better than using e.setAttribute I think.
const GoogleLoginButton = styled.img.attrs(
props => ({'src': props.img})
)`
display: inline;
width: 190px;
height: 45px;
&:hover {
cursor: pointer;
}
`;
const GoogleLoginButtonHover = styled.img.attrs(
props => ({'src': props.img})
)`
display: none;
width: 190px;
height: 45px;
&:hover {
cursor: pointer;
}
`;
const GoogleLoginButtonParent = styled.div`
&:hover ${GoogleLoginButtonHover} {
display: inline;
}
&:hover ${GoogleLoginButton} {
display: none;
}
`;
In your render you use it like this:
<GoogleLoginButtonParent>
<GoogleLoginButton
img = {props.img}
/>
<GoogleLoginButtonHover
img = {props.imgHover}
/>
</GoogleLoginButtonParent>
You can achieve this with pure CSS:
By replacing your img tag with a div and setting its CSS as follow:
div {
background: url('to_first_image');
}
div:hover {
background: url('to_second_image');
}
If you rather keep your img tag and use some JS:
onHover = (e) => {
e.setAttribute('src', 'source_to_first_img');
}
onUnhover = (e) => {
e.setAttribute('src', 'source_to_second_img');
}
Credit to this answer : https://stackoverflow.com/a/18032363/10449875

Resources