Can you pass props to a layout in sveltekit - sveltekit

I am working on a site with SvelteKit and I want to have a layout with a navbar for each page. The navbar has links with the active link being the page you are on. Is there a way I can pass a prop from each page to the template that changes which link is active?
Thanks!

I think I know what you're looking for. SvelteKit comes with the default page store. $page.url.pathname subscribes to that store and returns the current pathname. There are several ways you could apply this.
The following example creates a component Navlink.svelte, which
Imports the default Svelte store page
Creates a reactive boolean active and sets it to true when $page.url.pathname equals href. The $: make it reactive, meaning that Svelte reruns the code whenever either $page.url.pathname or href changes.
In the a element, we pass href as a prop. Whenever you use the Navlink component, you pass it href like you would a regular a element. That is also why we use export let href.
We also add class:active. This is the Svelte way of applying classes conditionally. Note that this is actually shorthand for class:active={active}, meaning we apply the class active when the variable active (after the equal sign) is true. When the variable and the class share the same name, we can use class:active. (docs on this subject)
Style the .active class however you like.
<script>
import { page } from "$app/stores"
export let href
$: active = $page.url.pathname === href
</script>
<a {href} class:active>
<slot />
</a>
<style>
a.active {
color: red;
}
</style>
You can also use a Tailwind class combined with some inline logic:
<a class:text-primary={$page.url.pathname === '/about'} href="/about">About</a>
Or use traditional CSS combined with a ternary operator:
<a href="/about" class="{($page.url.pathname === '/about')? active : inactive}"

Related

How to Override CSS of Inner Elements of Lightning Web Component

I have a dataTable component looke like below:
<template>
<lightning-datatable
class="pw-table"
key-field="id"
columns={columns}
data={data}
hide-checkbox-column
></lightning-datatable>
</template>
Now I want write some custom css to make table header higher,
.pw-table {
height: 300px;
}
but useless, so how I can do it?
I use loadStyle but failed, I dont know why?
import { loadStyle } from 'lightning/platformResourceLoader';
import accountCustom from '#salesforce/resourceUrl/accountCustom';
You should treat Lightning base components as black boxes. The internal elements of an LWC are protected by the Shadow DOM and Lightning Locker. You cannot style them with your CSS, and you cannot interact with them via JavaScript.
You can style the top-level element (which is actually what you've done here), but you can't style elements within the component. See CSS Style Sheets in the Lightning Web Components Dev Guide.
A parent component can style a child component, but it styles it as a single element.

How do I change the local host port when routing with Material-UI in react?

I am using a material-ui react button with the to={''} property to route to a new page with a different port.
The application runs on localhost:3000 but when I route with this button I want to change to localhost:8080. Right now I am doing it like to={'${process.env.UAA_HOST}/uaa/create_account'} which almost works by getting the correct port, but the problem is that it just adds it onto the localhost:3000 such that the route is: http://localhost:3000/http://localhost:8080/uaa/create_account
How do I replace just the port number?
React router's to method is a router-aware anchor, so it is trying to link within your current page. If you want an external link it is recommended to use a <a> tag. You could use this with the material-ui Button as follows:
const MyLink = () => <a href='${process.env.UAA_HOST}/uaa/create_account'/>
<Button component={MyLink}>
Link
</Button>
See this page of the Material-Ui docs for more details.
An alternative method, is to simply wrap your button in an <a> tag or just adding the href tag to your button.
ie:
<a href='${process.env.UAA_HOST}/uaa/create_account'> // this
<Button href='${process.env.UAA_HOST}/uaa/create_account' // or this>Link here</Button>
</a>

What's the react inline style for an anchor link?

I'm trying to set the standard properties of this anchor tag but I'm not sure of the syntax. How do I set the link color, visited color, hover, active etc}
const linkStyle = {
???
}
linkFormatter = cell => <a style={linkStyle} href='' role="link" onClick={() => { this.clickToGoToList(cell); }} >{cell} </a>;
So the short answer is that you cannot access selectors/pseudo-selectors with inline style objects. Possible solutions:
1) Style them with css in a stylesheet (though this feels pretty hacky)
2) Use a styling library. Glamorous is my preference but Bootstrap also has a React library. These libraries actually create React components to style elements and allow you access to pseudo-selectors like :active, :hover, and the like.

React Js :How does react route to different pages even though there is only one html file?

How does react does this internally ?
How are routes complied into normal javascript ? (I have not seen any routing libraries in normal javascript)
JavaScript can be used to manipulate the URL and your local page history without reloading the page, which is the main idea behind how React Router works (see pushState). Combine this with the fact that you can selectively show / hide content with JavaScript and you have React Router. For example:
js (with some jQuery syntactic sugar)
function toggle() {
$('#blockOne').toggleClass('hidden');
$('#blockOne').toggleClass('visible');
$('#blockTwo').toggleClass('hidden');
$('#blockTwo').toggleClass('visible');
}
css
.hidden { display: none }
.visible { display: block }
html
<div id="blockOne">First content</div>
<div id="blockTwo" class="hidden">Second content</div>
<button onclick="toggle()">Toggle</button>
Clicking the button would cause the first and second block to toggle their visibility. Of course, there's a bit more complexity with React Router, but this is the basic idea. Browser history manipulation + showing / hiding content with JS.

How to handle onclicks on hyperlinks most gracefully

What I want is rendered HTML that looks like this My cool Slug.
That means that someone can right-click on the link and open in a new tab. That'll bypass the fancy pushState stuff that react-router provides.
However, if someone makes a regular click, I need to update redux to say what the new slug is (or pagination page or whatever) AND call browserHistory.push(...).
There has to be a simpler way that is convenient. Something that does almost all of this without all the mess. Here's what my application looks like:
// imports
import { browserHistory } from 'react-router'
import changeSlug from './actions'
// the function
makeURL(thing) {
return `/page/${thing.slug}`
}
// this click method
handleClick(event, thing) {
event.preventDefault()
this.props.dispatch(changeSlug(thing.slug))
browserHistory.push(`/page/${thing.slug}`)
}
// the JSX
<a href={makeURL(myobject)}
onClick={(event) => handleClick(event, myobject)}
>Go to {myobject.title}</a>
Also, I tried using event.target in the event handler to get to the a.href attribute but because the <a> tag contains <span> elements, then event.target is the <span> tag I clicked inside the <a> element.
You can use <Link /> from react_router and determine onEnter/etc.
actions in your <Route /> (in <Router />).
Also you can wrap
your <a> in <span> and add some attributes to it and handle
click event.
Also you can add display: block style to <a> => it
shouldn't wraps by <span>

Resources