Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
preact-custom-element
Advanced tools
Generate/register a custom element from a preact component. As of 3.0.0, this library implements the Custom Elements v1 spec. Previous versions (< 3.0.0) implemented the v0 proposal, which was only implemented in Chrome and is abandoned.
Import register
and call it with your component, a tag name*, and a list of attribute names you want to observe:
import register from 'preact-custom-element';
const Greeting = ({ name = 'World' }) => (
<p>Hello, {name}!</p>
);
register(Greeting, 'x-greeting', ['name']);
* Note: as per the Custom Elements specification, the tag name must contain a hyphen.
Use the new tag name in HTML, attribute keys and values will be passed in as props:
<x-greeting name="Billy Jo"></x-greeting>
Output:
<p>Hello, Billy Jo!</p>
The Custom Elements V1 specification requires explictly stating the names of any attributes you want to observe. From your Preact component perspective, props
could be an object with any keys at runtime, so it's not always clear which props should be accepted as attributes.
If you omit the third parameter to register()
, the list of attributes to observe can be specified using a static observedAttributes
property on your Component. This also works for the Custom Element's name, which can be specified using a tagName
static property:
import register from 'preact-custom-element';
// <x-greeting name="Bo"></x-greeting>
class Greeting extends Component {
// Register as <x-greeting>:
static tagName = 'x-greeting';
// Track these attributes:
static observedAttributes = ['name'];
render({ name }) {
return <p>Hello, {name}!</p>;
}
}
register(Greeting);
If no observedAttributes
are specified, they will be inferred from the keys of propTypes
if present on the Component:
// Other option: use PropTypes:
function FullName(props) {
return <span>{props.first} {props.last}</span>
}
FullName.propTypes = {
first: Object, // you can use PropTypes, or this
last: Object // trick to define untyped props.
};
register(FullName, 'full-name');
FAQs
Wrap your component up as a custom element
The npm package preact-custom-element receives a total of 18,279 weekly downloads. As such, preact-custom-element popularity was classified as popular.
We found that preact-custom-element demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 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
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.