Skip to content

Instantly share code, notes, and snippets.

View dimfeld's full-sized avatar

Daniel Imfeld dimfeld

View GitHub Profile
@dimfeld
dimfeld / dropdown.svelte
Last active September 30, 2020 02:31
Very simplified version of nested dropdown tracking.
<!-- This is missing a bunch of the code that interacts with tippy to position the element, but
hopefully it makes sense -->
<script>
// The popup tracker makes sure that "outside click" handling doesn't trigger when a click occurs
// in a child dropdown, and the child dropdown's popup is fixed.
function makePopupTracker(parent) {
let contained: HTMLElement[] = [];
return {
contains(target) {
return contained.some((e) => e.contains(target));
@dimfeld
dimfeld / Dropdown.svelte
Created July 6, 2020 23:55
Dropdown Code
<script>
import tippy from 'tippy.js';
import 'tippy.js/animations/shift-away.css';
import 'tippy.js/dist/tippy.css';
import { createEventDispatcher, tick } from 'svelte';
import { scale } from 'svelte/transition';
import { cubicIn, cubicOut } from 'svelte/easing';
import Icon from './Icon.svelte';
import Button from './Button.svelte';
import { faChevronDown } from '@fortawesome/pro-solid-svg-icons/faChevronDown';
@dimfeld
dimfeld / immerStore.ts
Last active June 25, 2020 15:54
Immer Store
import { writable, Readable } from 'svelte/store';
import { produce, enableAllPlugins } from 'immer';
enableAllPlugins();
export interface ImmerStore<T> {
update: (updateFn: (draft: T) => T | void) => T;
subscribe: Readable<T>['subscribe'];
}
@dimfeld
dimfeld / Babel Configs
Last active June 5, 2020 02:10
Babel Config Sample
const babelServerConfig = {
extensions: ['.js', '.mjs', '.html', '.ts'],
babelHelpers: 'bundled',
exclude: ['node_modules/@babel/**'],
presets: [
[
'@babel/preset-env',
{
modules: false,
targets: { node: 12 },
@dimfeld
dimfeld / mostVisibleElement.ts
Created May 28, 2020 19:25
Svelte IntersectionObserver action example
/**
* This is intended to be used with the svelte `use` syntax. It examines all
* child elements of the element that match `selector`, and calls the callback
* when the child element with the highest percentage overlap with the visible
* part of the container changes.
* <div use:mostVisibleElement={{ cb: setActive }}><section><section></div>
*/
export default function mostVisibleElement(
container: Element,
{ selector, cb }
export default function (
Component: typeof SvelteComponent,
events?: { [svelteEvent: string]: string }
) {
return class {
$element: Element[];
initialProps: { [key: string]: any };
component: SvelteComponent;
constructor($element) {
'ngInject';
<div class="px-2 flex flex-col">
<!-- Selected: adds bg-gray-200, removes bg-gray-100 -->
<!-- First row: Removes mt-1 -->
<div class="bg-gray-200 rounded-md px-2">
<span>Content</span>
<!-- Selected: adds bg-gray-100, removes bg-gray-200 -->
<span class="ml-auto bg-gray-100">5</span>
</div>
@dimfeld
dimfeld / days_until_earnings
Created November 21, 2019 18:50
Thinkscript for Days until Earnings
input length = 60;
def xx = -GetEventOffset(Events.EARNINGS);
def yy = Sum(HasEarnings(type = EarningTime.AFTER_MARKET), length)[-length + 1] > 0;
plot x = xx + yy * .5;
x.AssignValueColor( if x <= 1 then Color.LIGHT_RED else Color.DARK_GRAY);
@dimfeld
dimfeld / slog-logging-middleware.rs
Last active August 27, 2021 04:50
Simple actix-web middleware for custom KV logging with slog
use actix_service::{Service, Transform};
use actix_web::{dev::ServiceRequest, dev::ServiceResponse, Error};
use futures::future::{ok, FutureResult};
use futures::{Future, Poll};
use slog::info;
// There are two step in middleware processing.
// 1. Middleware initialization, middleware factory get called with
// next service in chain as parameter.
// 2. Middleware's call method get called with normal request.
@dimfeld
dimfeld / index.js
Last active June 13, 2018 21:49
Typescript compiler crash on redundant export
import { abc } from './redundant_export';
abc(1, 2, 3);