![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
fela-identifier
Advanced tools
This enhancer allows you to create rules for which the renderer will generate unique class names.
yarn add fela-identifier
You may alternatively use npm i --save fela-identifier
.
Sometimes it is necessary to additionally stylize (for example, highlight) some DOM element when manipulating (for example, when hovering) with the parent. In classical CSS this would look something like this:
.parent-element:hover .child-element {
background-color: red;
}
But with Fela, this is not so easy, because you do not assign classes yourself.
Create a rule for which the renderer will predictably generate the same unique class name.
import { createRenderer } from 'fela'
import createIdentifier from 'fela-identifier'
const identifier = createIdentifier()
const renderer = createRenderer({
enhancers: [ identifier ]
})
const childElementRule = identifier()
const parentElementRule = () => ({
[`:hover .${childElementRule}`]: {
backgroundColor: 'red',
}
})
renderer.renderRule(childElementRule)
// => 'fela-identifier-0'
renderer.renderRule(parentElementRule)
// => 'a'
outputs
.a:hover .fela-identifier-0 {
background-color: red
}
The module exports a function that creates an identifier
. The identifier
is both an enhancer and a factory of unique rules. As a factory of unique rules, the identifier
has the following API:
name
(String): This is a string that can be used to produce an identifier. Sometimes this is convenient for debugging or visibility.(Function): A Fela rule that can be rendered. It also has a field className
and an overridden method toString
that returns the same value as the field className
. You can use it like this:
import createIdentifier from 'fela-identifier'
const identifier = createIdentifier()
const rule = identifier()
const className = rule.className
rule.toString()
!!!Attention!!!
Do not use the identifier
as a factory before you connect it as an enhancer. This will result in a runtime error.
Option | Value | Default | Description |
---|---|---|---|
prefix | (string) | fela-identifier | a prefix to be inserted at the beginning of the identifying class |
generator | (name : string, index : number) => string | just returns the index | function that is responsible for generating identifiers based on the name and index |
import { createRenderer } from 'fela'
import createIdentifier from 'fela-identifier'
import uuidv4 from 'uuid/v4'
import hash from 'object-hash'
const identifier = createIdentifier({
prefix: 'my-custom-prefix',
generator: (name, index) => `${hash([name, index])}-${uuidv4()}`,
})
const renderer = createRenderer({
enhancers: [ identifier ]
})
Usage with react-fela
:
import * as React from 'react'
import * as ReactDOM from 'react-dom'
import { Provider, connect } from 'react-fela'
import { createRenderer } from 'fela'
import createIdentifier from 'fela-identifier'
export const identifier = createIdentifier()
export const renderer = createRenderer({
enhancers: [ identifier ],
})
let Component = ({ styles }) => (
<div className={styles.parent}>
Some Parent Content
<div className={styles.child}>
Some Child Content
</div>
</div>
)
const child = identifier()
Component = connect({
parent: {
[`:hover .${child}`]: {
backgroundColor: 'red',
}
},
child
})(Component)
ReactDOM.render(
<Provider renderer={renderer}>
<Component />
</Provider>,
document.getElementById("root"),
)
You can combine identifying rules with styles and with each other:
import { createRenderer, combineRules } from 'fela'
import createIdentifier from 'fela-identifier'
export const identifier = createIdentifier()
export const renderer = createRenderer({
enhancers: [ identifier ],
})
const firstId = identifier('first')
const secondId = identifier('second')
const someStyle = () => ({
fontSize: '20px',
backgroundColor: 'red',
})
const combinedRule = combineRules(
firstId,
secondId,
someStyle,
)
renderer.renderRule(combinedRule)
// => 'a b fela-identifier-first-0 fela-identifier-second-1'
Fela is licensed under the MIT License.
Documentation is licensed under Creative Common License.
Created with ♥ by @rofrischmann and all the great contributors.
FAQs
Fela enhancer to creation identifying rules
The npm package fela-identifier receives a total of 187 weekly downloads. As such, fela-identifier popularity was classified as not popular.
We found that fela-identifier demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.