Skip to content

Commit c84a7cb

Browse files
committed
texto, #4: adds draft of SelectionMenu
1 parent 1f98bbc commit c84a7cb

File tree

8 files changed

+152
-14
lines changed

8 files changed

+152
-14
lines changed

components/Logo.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import styled from 'styled-components';
22

33
import Image from './layout/Image';
44
import { iconSize } from './theme';
5-
import { BACKEND_URL } from '../config/client.js';
65

76
type LogoProps = {
87
dim?: number;

components/Navbar.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { createRef, Component, RefObject } from 'react';
55
import Logo from './Logo';
66
import { zIndex } from './theme';
77
import { linkHighlight } from './mixins';
8-
import { BACKEND_URL } from '../config/client.js';
8+
import { BACKEND_URL } from '../config/client';
99

1010
export const navbarHeight = 4;
1111

components/button/EmailMeButton.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Link from 'next/link';
22

33
import Button from './Button';
4-
import { EMAIL, BACKEND_URL } from '../../config/client.js';
4+
import { EMAIL, BACKEND_URL } from '../../config/client';
55

66
const onMailto = (href: string) => () => window.location.assign(href);
77

components/section/Section.tsx

+6-6
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const sectionTypes = {
2424
`,
2525
secondary: `
2626
color: black;
27-
background-color: rgba(0,0,0,0.05);
27+
background-color: white;
2828
`,
2929
};
3030

@@ -39,7 +39,7 @@ export type SectionProps = {
3939
backgroundPosition?: React.CSSProperties['backgroundPosition'];
4040
};
4141

42-
const Section = styled<SectionProps, 'section'>('section')`
42+
const _Section = styled<SectionProps, 'section'>('section')`
4343
${boxShadow()}
4444
${align('v-center')}
4545
${forMedia('phone', 'padding: 2rem;')}
@@ -52,14 +52,14 @@ const Section = styled<SectionProps, 'section'>('section')`
5252
position: relative;
5353
`;
5454

55-
const _Section: React.SFC<SectionProps> = ({
55+
const Section: React.SFC<SectionProps> = ({
5656
background,
5757
nextSectionLink,
5858
backgroundPosition,
5959
children,
6060
...props
6161
}) => (
62-
<Section {...props}>
62+
<_Section {...props}>
6363
<Background
6464
position={backgroundPosition}
6565
{...parseBackgroundAsString(background)}
@@ -68,7 +68,7 @@ const _Section: React.SFC<SectionProps> = ({
6868
{children}
6969

7070
{props.id && <SectionLink {...props} />}
71-
</Section>
71+
</_Section>
7272
);
7373

74-
export default _Section;
74+
export default Section;

package.json

+6-5
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,18 @@
2020
"@types/react": "^16.4.4",
2121
"@types/react-dom": "^16.0.6",
2222
"@zeit/next-typescript": "^1.1.0",
23+
"@types/segment-analytics": "^0.0.28",
2324
"babel-plugin-styled-components": "^1.5.1",
24-
"next": "^6.1.0",
25-
"react": "^16.4.1",
26-
"react-dom": "^16.4.1",
27-
"styled-components": "^3.3.3",
2825
"tslint": "^5.10.0",
2926
"tslint-config-airbnb": "^5.9.2",
3027
"tslint-react": "^3.6.0",
3128
"typescript": "^2.9.2"
3229
},
3330
"dependencies": {
34-
"@types/segment-analytics": "^0.0.28"
31+
"next": "^6.1.0",
32+
"react": "^16.4.1",
33+
"txtgen": "^2.2.2",
34+
"react-dom": "^16.4.1",
35+
"styled-components": "^3.3.3"
3536
}
3637
}

pages/portfolio/texto.tsx

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import styled from 'styled-components';
2+
import * as txtgen from 'txtgen';
3+
import { Component } from 'react';
4+
5+
import Text from '../../components/Text';
6+
import Page from '../../components/Page';
7+
import { H2 } from '../../components/Heading';
8+
import { HR } from '../../components/Spacing';
9+
import Section from '../../components/section/Section';
10+
import { content, gutter } from '../../components/mixins';
11+
import { zIndex } from '../../components/theme';
12+
13+
function onSelectionChange(component?: TextoPage) {
14+
console.log('-- \nselectionchange', component);
15+
16+
const selection = window.getSelection();
17+
const selectionRange = selection.getRangeAt(0);
18+
19+
const rect = selectionRange.getBoundingClientRect();
20+
const selectedText = selection.toString();
21+
22+
component.setState({
23+
selectedText,
24+
menuTop: rect.top - rect.height - 30,
25+
menuLeft: rect.left,
26+
isMenuOpen: !!selectedText.trim(),
27+
});
28+
}
29+
30+
type SelectMenuProps = {
31+
top: number;
32+
left: number;
33+
isMenuOpen: boolean;
34+
};
35+
36+
const SelectMenu = styled<SelectMenuProps, 'div'>('div')`
37+
z-index: ${zIndex.dialog};
38+
39+
top: ${({ top }) => top}px;
40+
left: ${({ left }) => left}px;
41+
position: fixed;
42+
43+
${({ isMenuOpen }) => `
44+
display: ${isMenuOpen ? 'block' : 'none'};
45+
`}
46+
47+
color: snow;
48+
background: rgba(0,0,0,0.9);
49+
border-radius: 4px;
50+
${gutter(0.25)}
51+
`;
52+
53+
type State = {
54+
menuTop: SelectMenuProps['top'];
55+
menuLeft: SelectMenuProps['left'];
56+
isMenuOpen?: boolean;
57+
selectedText?: string;
58+
};
59+
60+
const contentText = txtgen.article();
61+
62+
class TextoPage extends Component<{}, State> {
63+
64+
state: State = {
65+
menuTop: 0,
66+
menuLeft: 0,
67+
isMenuOpen: false,
68+
};
69+
70+
componentDidMount() {
71+
document.addEventListener('selectionchange', () => onSelectionChange(this));
72+
}
73+
74+
componentWillUnmount() {
75+
document.removeEventListener('selectionchange', () => onSelectionChange(this));
76+
}
77+
78+
render() {
79+
const {
80+
menuTop,
81+
menuLeft,
82+
isMenuOpen,
83+
selectedText,
84+
} = this.state;
85+
86+
return (
87+
<Page title="Text select">
88+
<Section variation="secondary" mixins={[content()]}>
89+
<SelectMenu
90+
top={menuTop}
91+
left={menuLeft}
92+
isMenuOpen={isMenuOpen}
93+
>
94+
{selectedText}
95+
</SelectMenu>
96+
97+
<H2>
98+
Texto
99+
</H2>
100+
<HR />
101+
102+
<Text>
103+
Text lookup and relationships to other applications is a rather{' '}
104+
normal feature in different platforms. Different content, like text, is{' '}
105+
forwared between applications either copying and pasting it. What if we can{' '}
106+
do the same with an arbitrary service? As much as there are privacy complications here{' '}
107+
there is also simple and interesting applications to it.
108+
</Text>
109+
110+
<pre>
111+
What follows is random text, select words to see a response.
112+
</pre>
113+
114+
<Text>
115+
{contentText}
116+
</Text>
117+
</Section>
118+
</Page>
119+
);
120+
}
121+
}
122+
123+
export default TextoPage;

types/txtgen.d.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/** Declaration file generated by dts-gen */
2+
3+
declare module 'txtgen' {
4+
export function sentence(): string;
5+
export function article(len?: number): string;
6+
export function paragraph(len?: number): string;
7+
export function addNouns(ls: string[]): string;
8+
export function addTemplates(ls: string[]): string;
9+
export function addAdjectives(ls: string[]): string;
10+
}
11+

yarn.lock

+4
Original file line numberDiff line numberDiff line change
@@ -4064,6 +4064,10 @@ [email protected]:
40644064
version "0.0.0"
40654065
resolved "https://registry.yarnpkg.com/tty-browserify/-/tty-browserify-0.0.0.tgz#a157ba402da24e9bf957f9aa69d524eed42901a6"
40664066

4067+
txtgen@^2.2.2:
4068+
version "2.2.2"
4069+
resolved "https://registry.yarnpkg.com/txtgen/-/txtgen-2.2.2.tgz#22da4cc3d69a4c48915db312119b53494e6b24ef"
4070+
40674071
typedarray@^0.0.6:
40684072
version "0.0.6"
40694073
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"

0 commit comments

Comments
 (0)