Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Never mix business logic and styles again!
Chameleon is a library that allows you write components that are styled differently depending on where they are in your app. A <Header>
component might render at 40px by default, or at 35px within a <Panel>
. It might be colored blue by default, or yellow in night mode.
Styles changes in Chameleon are handled very simply. Style information is passed down through React's context. This context is updated using a contextReducer
. Presentational components read that context and render appropriately. That's it!
The goal of Chameleon is to allow you to completely separate styles from business logic.
In this quick overview, we'll use Chameleon to make <Section>
and <Header>
components. By default, a <Header>
component will render at 40px. However, within a <Section>
, it ·will render at 35px. Within two nested sections, it will render at 30px, and so on.
<UpdateContext>
and <ContextProvider>
components. To do this, you need to use a reducer and plug it into makeContextComponents
. Let's write one:// StyleContext.js
import { makeContextComponents } from 'chameleon';
const initialContext = { sectionDepth: 0 };
const contextReducer = (previousContext, { type }) => {
if (type === 'INCREMENT_SECTION_DEPTH') {
return {
...previousContext,
sectionDepth: previousContext.sectionDepth + 1,
};
}
return previousContext;
};
const { UpdateContext, ContextProvider } = makeContextComponents(contextReducer, initialContext);
export {
UpdateContext,
ContextProvider,
};
Section
component that wraps UpdateContext
.// Section.js
export default ({ children }) => (<UpdateContext type="INCREMENT_SECTION_DEPTH">
{ children }
</UpdateContext>);
sectionDepth
from the context.// Header.js
export default const ({ children }) => (<ContextProvider>{context => {
const Header = styled.span`
fontSize: ${40 - context.sectionDepth * 5}px;
`;
return <Header>{ children }</Header>;
}}</ContextProvider>);
const MyArticle = () => (<div>
<Header>How To Use Chameleon</Header>
<Section>
<Header>This subheader has a font size of 35 pixels!</Header>
<Section>
<Header>This sub-sub-header has a font size of 30 pixels! Neat!</Header>
</Section>
</Section>
</div>);
yarn add chameleon
Chameleon is for managing all aspects of style. For example:
pointer-events
and modify the cursor
for a section of the site that is disabled, or which is behind a modal.<FormGroup>
can render differently within an <InlineForm>
or within a <Form>
.For the sake of brevity, the minimal example omits several patterns and advanced features.
<UpdateContext>
. In the minimal example, there's no reason to simplify the API. But once you start passing more parameters to <UpdateContext>
, you will benefit from components like the following:// Nightmode.js
import React from 'react';
import { UpdateContext } from './StyleContext';
const NightMode = ({ children }) => <UpdateContext
type="CHANGE_MODE"
bgColor="black"
color="white"
{/* etc */}
>
{ children }
</UpdateContext>
Note: You can also consider simplifying your action's API. type="CHANGE_MODE" mode="NIGHT"
might be sufficient, and not require a wrapping component.
// StyleContext.js
import styled from 'styled-components';
class StyleContext {
constructor(context) {
this.context = context;
}
get foregroundStyleFromMode() {
return ({
NIGHT: `
color: yellow;
&:hover {
color: gold;
}
`,
DAY: `
color: blue;
&:hover {
color: cyan;
}
`,
})[this.context.mode];
}
get headingStyleFromSectionDepth() {
return `
font-size: ${40 - 5 * this.context.sectionDepth}px;
line-height: 1em;
font-weight: ${800 - 100 * this.context.sectionDepth};
margin-bottom: ${20 - 2 * this.context.sectionDepth}px;
`;
}
get Header() {
return styled.span`
${this.foregroundStyleFromMode}
${this.headingStyleFromSectionDepth}
`;
}
incrementSectionDepth() {
return new StyleContext({
...this.context,
sectionDepth: this.context.sectionDepth + 1,
});
}
}
const contextReducer = (previousContext, action) => {
switch (action.type) {
case "INCREMENT_SECTION_DEPTH":
return previousContext.incrementSectionDepth();
// ... case "CHANGE_MODE": ...
}
return previousContext;
}
const initialContext = new StyleContext({
mode: 'DAY',
sectionDepth: 0,
});
const {
UpdateContext,
ContextProvider,
} = makeContextComponents(contextReducer, initialContext);
// Header.js
export default (props) => (<ContextProvider>{ context =>
<context.Header { ...props } />
}</ContextProvider>);
Let's take a minute to appreciate what we've achieved here. We have a <Header>
component that is styled correctly in night and day modes, and which gets progressively smaller as it is wrapped in more sections. It even has correct hover behavior, thanks to the magic of styled components!
Furthermore, we have a clean API (contextReducer
) for making multiple updates to the context! This means that, whenever we use a component that updates the context (a <Panel>
, or a component that indicates a modal has been overlaid), we don't need to worry about the whole context. We just need to know about the incremental change that we are making.
Lastly, we have set ourselves up for maximal code reuse. Many components (buttons, headings, emphasized text, etc.) might re-use the same colors, allowing us to reuse get foregroundStyleFromMode
.
updateContextGenerator
. makeContextComponents
also returns updateContextGenerator
, which is a convenient wrapper around <UpdateContext>
. Example:const { UpdateContext, updateContextGenerator } = makeContextComponents(contextReducer, initialContext);
// the following are equivalent:
const IncrementSectionDepth = updateContextGenerator({ type: 'INCREMENT_SECTION_DEPTH' });
const IncrementSectionDepth = ({ children }) => (<UpdateContext type="INCREMENT_SECTION_DEPTH">
{ children }
</UpdateContext>);
propertyComponentGenerator
. makeContextComponents
also returns propertyComponentGenerator
, which is a convenient wrapper for very simple components. For example, the following are equivalent:const Header = ({ ...props }) => (<ContextProvider>{ context =>
<context.Header { ...props } />
}</ContextProvider>);
const Header = propertyComponentGenerator(context => context.Header);
Combine <ContextProvider>
and <UpdateContext>
to make components (such as <Panel>
's, <Section>
's and <Modal>
's) which are styled in a specific way and which also update their context.
Throw errors in your contextReducer
if you're entering an invalid state. If, for example, you don't want modals within modals, ensure that with:
case "ENTER_MODAL":
if (previousContext.isInModal) {
throw new Error('Cannot render nested modals');
}
return {
...previousContext,
isInModal: true,
};
You're right. Do what's right for your project!
FAQs
> Never mix business logic and styles again!
We found that chameleon demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.