npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jota-ds/context-element-react

v1.0.0

Published

A LitElement overlay to support contexts

Downloads

3

Readme

Context Element React

Context Element React extends Context Element to add native React support.

Installation

This package is only available on Github NPM registry.

yarn add @jota-ds/context-element-react

Getting Started

What Is Context?

Context is a way of represent style variables (such as border-radius, background-color, etc.) so that they are dynamic and easy to change at runtime. In this way, it is possible to generate and control visual changes more easily.

Building a Context Element

Body

// index.js
import { ContextElement } from '@meiuca/context-element-react';
import style from './styles.js';

export default function MyInput({ required, placeholder, type, label }) {
  return (
    <ContextElement contextId={`my-input`} styles={style}>
      <label className={`my-Input-label`}>
        <span>{label}</span>
        <input
          className={`my-Input`}
          type={type || 'text'}
          placeholder={placeholder}
          required={required}
        />
      </label>
    </ContextElement>
  );
}

Style

// style.js
import { createGooberGetter as css } from '@meiuca/context-element';
import { inputContext } from './contexts.js';

export default css(inputContext)`
  &.my-Input-label {
    display: flex;
    flex-direction: column;

    span {
      color: ${({ label }) => label.color};
      font-size: ${({ label }) => label.fontSize};
      font-weight: ${({ label }) => label.fontWeight};
      font-family: ${({ label }) => label.fontFamily};
      margin-bottom: ${({ label }) => label.margin};
      line-height: ${({ label }) => label.lineHeight};
    }

    input {
      border-style: ${context => context.borderStyle};
      border-width: ${context => context.borderWidth};

      border-color: ${context => context.borderColor};
      padding: ${context => context.padding};
      background-color: transparent;
      outline: none;

      ::placeholder {
        color: ${({ placeholder }) => placeholder.color};
        font-size: ${({ placeholder }) => placeholder.fontSize};
        font-weight: ${({ placeholder }) => placeholder.fontWeight};
        font-family: ${({ placeholder }) => placeholder.fontFamily};
        line-height: ${({ placeholder }) => placeholder.lineHeight};
      }
    }
  }
`;

Context Element React also exports the createLitGetter, which generates a compatible getter without using Goober.

How does it work?

ContextElement is a wrapper that handles context. See ContextElement.

<ContextElement styles={...}> accepts a single (or an array of) StyleGetter. Default is [].

<ContextElement insertOnIndex={...}> accepts number, number[] or boolean. Default is true.

If boolean, controls whether styleIdList will be inserted in all direct children or not

If number, insert the styleIdList into the direct child whose index is equal to number

If number[], follows the same rule as number but with support for multiple indexes

createGooberGetter is a template string tag that uses the CreateStyleGetterProps interface, thus, the first interpolation must be used to inject the default context, and the other interpolations must use callbacks to obtain it; in this way, context values ​​can be changed at runtime (via setContext).

The Context Element creates two global objects: DSRegistry and DSContext.

DSRegistry is an array where all instances of ContextElement register itself, thus being able to be easily accessed by other entities, such as updateRegisteredComponents function.

DSContext is a map that can contain the contexts of the components. Initially it is an empty instance, but it is used by setContext to store contexts that will override their respective default. The override is made with the merge strategy.

When updated, the ContextElement will try to get their context from DSContext and will merge it with the default context. Then will pass it on to the callbacks.

Note that, unlike Context Element (WC), the component's context pointer (this.contextId) is set directly by <ContextElement contextId={...}>.

setContext

This function has 4 overloads.

setContext('https://url.to.global.context/'); // `https://url.to.global.context/` will return `{"my-input": {"color": "#ddd"}}`

setContext({ 'my-input': { color: '#ddd' } });

setContext('my-input', { color: '#ddd' });

setContext('my-input', 'https://url.to.component.context/'); // `https://url.to.component.context/` will return `{"color": "#ddd"}`

So let's say MyInput has, as a default context, {color: "blue", fontSize: "20px"}; after being instantiated, its context will be {color: "#ddd", fontSize: "20px"}. If this.contextId is changed at some point, the instance will lose the previous reference and, if the new pointer does not exist in DSContext, the default context will take over.

Contributing

Any contribution is welcome, as long as you follow the rules of prettier, eslint, commitlint, tests and git branch

Prettier

all files must be formatted using prettier, whose behavior is determined by .prettierrc

Eslint

extends @open-wc/eslint-config. See .eslintrc

Commitlint

extends @commitlint/config-conventional and adds the following scopes (see commitlint.config.js):

  • eslint: any eslint config or dependency change

  • prettier: any prettier config or dependency change

  • babel: any babel config or dependency change

  • commitlint: any commitlint config or dependency change

  • snowpack: any snowpack config or dependency change

  • web-test-runner: any web-test-runner config or dependency change

  • husky: any husky config or dependency change

  • husky-controller: any husky-controller config or dependency change

  • server: any change to the server.js file

  • vscode: any change to the .vscode folder

  • ('./src/*.js', 'src#'): any change to any js file inside src folder; prefixed by src#. Example: src#context.js

  • ('./test/*.js', 'test#'): any change to any js file inside test folder; prefixed by test#

  • package-json: any change to the package.json file that do not affect any of the previous scopes

Before committing, please make sure that husky is properly installed, Especially if you are a mac user.