Skip to content

Instantly share code, notes, and snippets.

@na0x2c6
na0x2c6 / some-component.ts
Created March 22, 2020 11:00
LitElement with embed a scss (for IE).
import { LitElement, html, unsafeCSS } from 'lit-element';
import style from './scss/style.scss';
@customElement('some-component')
export default class SomeComponent extends LitElement {
static get styles() {
return [
// If you don't need to consider IE, you can use a link tag to include style in the render function.
unsafeCSS(style),
]
@na0x2c6
na0x2c6 / index.html
Last active April 5, 2020 04:20
The set for building an app using LitElement, considering IE.
<script type="text/javascript">
// Write me in the head tag.
// For this sample, you need to put `@webcomponents/webcomponentsjs/` dirctory from the node_modules into `your-doc-root/js/` dirctory.
(function() {
if (('customElements' in window)) {
document.write('<script defer type="text/javascript" src="/js/main.js"><\/script>');
} else {
document.write('<script defer type="text/javascript" src="/js/@webcomponents/webcomponentsjs/webcomponents-loader.js"><\/script>');
window.WebComponents = window.WebComponents || {};
window.WebComponents.root = '/js/@webcomponents/webcomponentsjs/';
@na0x2c6
na0x2c6 / calcOffset.ts
Created March 22, 2020 07:21
Calculating an element node offset.
function calcOffset(elm: HTMLElement) {
const offset = {
top: 0,
left: 0,
}
do {
offset.top += elm.offsetTop || 0;
offset.left += elm.offsetLeft || 0;
} while((elm = elm.offsetParent as HTMLElement));
@na0x2c6
na0x2c6 / functions.php
Created March 21, 2020 11:08
WordPress: get the paged number
<?php
add_action('wp_head', function() {
$paged = get_query_var('paged');
});
@na0x2c6
na0x2c6 / functions.php
Last active March 21, 2020 10:02
WordPress: An example for adding noindex attribute to a page
<?php
add_action( 'wp_head', function() {
if(!(is_single() || is_page())){
return;
}
$value = get_post_meta(get_the_ID(), 'is_noindex', true );
if(!((int)$value)) {
return;
}
wp_no_robots();
@na0x2c6
na0x2c6 / HOCSample-3_2.jsx
Created October 31, 2018 02:32
Inheritance Inversion Sample 2
function addPrefixToName(WrappedComponent, prefix) {
return class extends WrappedComponent {
render() {
const wrappedTree = super.render()
let newProps = {}
if (wrappedTree && wrappedTree.type === 'input') {
newProps = {name: `${prefix}-${wrappedTree.props.name}`}
}
const props = { ...wrappedTree.props, ...newProps } // Object.assign({}, wrappedTree.props, newProps) と同様
const newTree = React.cloneElement(wrappedTree, props, wrappedTree.props.children)
@na0x2c6
na0x2c6 / HOCSample-3_1.jsx
Created August 28, 2018 09:21
Inheritance Inversion Sample 1
function showMessageWhenDataIsEmpty(WrappedComponent) {
return class extends WrappedComponent {
render() {
if (this.props.data.length > 0) { // データが存在するときだけ表示
return super.render()
}
else {
return <span>データが存在しません!</span>
}
}
@na0x2c6
na0x2c6 / HOCSample-3.jsx
Created August 28, 2018 07:55
HOC Sample 3:Inheritance Inversion
function withSubscription(WrappedComponent, selectData) {
return class extends WrappedComponent {
constructor(props) {
super(props)
this.handleChange = this.handleChange.bind(this)
this.state = {
data: selectData(DataSource, props)
}
}
function withSubscription(WrappedComponent, selectData) {
return class extends React.Component {
constructor(props) {
super(props)
this.handleChange = this.handleChange.bind(this)
this.state = {
data: selectData(DataSource, props)
}
}
@na0x2c6
na0x2c6 / HOCSample-2.jsx
Last active August 24, 2018 09:09
HOC Sample 2: Props Proxy
function withSubscription(WrappedComponent, selectData) {
return class extends React.Component {
constructor(props) {
super(props)
this.handleChange = this.handleChange.bind(this)
this.state = {
data: selectData(DataSource, props)
}
}