Skip to content

Commit b972818

Browse files
committed
Improve transitions and add icons
1 parent ed04bd0 commit b972818

File tree

10 files changed

+278
-95
lines changed

10 files changed

+278
-95
lines changed

cypress/e2e/generic/public-comment.cy.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,13 @@ describe("Guest comment", { testIsolation: false }, () => {
4343
const loggedInEditText = generateRandomCopy()
4444
cy.intercept("PUT", ".netlify/functions/comment/*").as("putComment")
4545
cy.get(".overflow-menu-button").first().click()
46-
cy.get(".comment-edit-button").first().click()
47-
cy.get("form.comment-form .comment-field").clear().type(loggedInEditText)
48-
cy.get("form.comment-form .comment-update-button").click()
46+
cy.get(".comment-edit-button").click()
47+
cy.get("li.comment.is-open form.comment-form .comment-field")
48+
.clear()
49+
.type(loggedInEditText)
50+
cy.get(
51+
"li.comment.is-open form.comment-form .comment-update-button"
52+
).click()
4953
cy.wait("@putComment").its("response.statusCode").should("eq", 204) // 204
5054
cy.contains("article.comment-body p", loggedInEditText).should("exist")
5155
})
@@ -65,13 +69,19 @@ describe("Guest comment", { testIsolation: false }, () => {
6569
} else req.reply()
6670
}).as("putComment")
6771
cy.get(".overflow-menu-button").first().click()
68-
cy.get(".comment-edit-button").first().click()
72+
cy.get(".comment-edit-button").click()
73+
cy.get("li.comment.is-open form.comment-form .comment-field")
74+
.clear()
75+
.type(errorText)
76+
cy.get(
77+
"li.comment.is-open form.comment-form .comment-update-button"
78+
).click()
6979

70-
cy.get("form.comment-form .comment-field").clear().type(errorText)
71-
cy.get("form.comment-form .comment-update-button").click()
7280
cy.wait("@putComment")
7381

74-
cy.get("form.comment-form .comment-update-button").click()
82+
cy.get(
83+
"li.comment.is-open form.comment-form .comment-update-button"
84+
).click()
7585

7686
cy.wait("@putComment").then(interception => {
7787
expect(interception.response.statusCode).to.equal(204)
@@ -102,10 +112,12 @@ describe("Guest comment", { testIsolation: false }, () => {
102112
})
103113

104114
it("Reply to a comment as a logged-in guest", () => {
105-
cy.get("button.comment-reply-button").first().as("replyButton")
106-
cy.get("@replyButton").closest("article.comment-body").as("commentBody")
107-
cy.get("@replyButton").click()
108-
cy.get("form.guest-login-form").should("not.exist")
115+
const commentText = generateRandomCopy()
116+
cy.get("button.comment-reply-button").first().click()
117+
// cy.wait(500)
118+
cy.get("textarea.comment-field").should("have.length", 1).type(commentText)
119+
cy.get("button.comment-submit-button").click()
120+
cy.get("article.comment-body p").contains(commentText).should("exist")
109121
})
110122

111123
it("Can log out", () => {

cypress/e2e/generic/rtl.cy.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,10 @@ describe("Simple Comment right-to-left rendering", () => {
114114
})
115115
})
116116
cy.visit("/")
117-
cy.get("li#http-localhost-7070_hvh-21s6-c1m6p")
118-
.first()
119-
.should("have.class", "is-rtl")
117+
cy.get("li#http-localhost-7070_hvh-21s6-c1m6p").should(
118+
"have.class",
119+
"is-rtl"
120+
)
120121
})
121122

122123
it("properly renders Arabic", () => {
@@ -143,9 +144,9 @@ describe("Simple Comment right-to-left rendering", () => {
143144
})
144145
})
145146
cy.visit("/")
146-
cy.get("li#http-localhost-7070_hvh-21s6-c1m6p")
147-
.children("article.comment-body")
148-
.first()
149-
.should("not.have.class", "is-rtl")
147+
cy.get("li#http-localhost-7070_hvh-21s6-c1m6p").should(
148+
"not.have.class",
149+
"is-rtl"
150+
)
150151
})
151152
})

src/components/CommentDisplay.svelte

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
import {
1717
ClosedCaptionAlt as ReplyIcon,
1818
Edit as EditIcon,
19-
OverflowMenuHorizontal,
20-
ChevronLeft,
19+
OverflowMenuHorizontal as OverflowMenuIcon,
20+
ChevronLeft as ChevronLeftIcon,
2121
} from "carbon-icons-svelte"
22-
import { linear } from "svelte/easing"
22+
import { open } from "../lib/svelte-transitions"
2323
2424
export let comment: (Comment & { isNew?: true }) | undefined = undefined
2525
export let showReply: string
@@ -92,30 +92,6 @@
9292
9393
if (showReply !== comment.id) isOverflowToggled = false
9494
}
95-
96-
const toggleExpand = (
97-
_overflowMenu: HTMLElement,
98-
params?: {
99-
delay: number
100-
duration: number
101-
easing: (t: number) => number
102-
direction: "in" | "out" | "both"
103-
}
104-
) => {
105-
const { delay = 0, duration = 250, easing = linear } = params
106-
107-
const css = (t: number, u: number) => {
108-
const maxWidth = (1 - u) * 100
109-
return `max-width: ${maxWidth}%`
110-
}
111-
112-
return {
113-
delay,
114-
duration,
115-
easing,
116-
css,
117-
}
118-
}
11995
</script>
12096

12197
<li
@@ -178,7 +154,7 @@
178154
{#if isEditing}
179155
<CommentEdit
180156
placeholder="Your edit"
181-
autofocus={isRoot ? true : false}
157+
autofocus
182158
commentId={comment.id}
183159
commentText={comment.text}
184160
onCancel={onCancelEditClick}
@@ -192,7 +168,7 @@
192168
{#if showReply === comment.id && !isEditing}
193169
<CommentInput
194170
placeholder="Your reply"
195-
autofocus={isRoot ? true : false}
171+
autofocus
196172
commentId={comment.id}
197173
{currentUser}
198174
onCancel={onCloseCommentInput}
@@ -211,7 +187,7 @@
211187
<ReplyIcon size={20} /> Reply
212188
</button>
213189
{#if isOverflowToggled}
214-
<div class="overflow-menu" in:toggleExpand out:toggleExpand>
190+
<div class="overflow-menu" transition:open>
215191
{#if canEdit(comment)}
216192
<button
217193
on:click={() => onEditClick(comment)}
@@ -233,15 +209,15 @@
233209
{/if}
234210

235211
<button on:click={() => (isOverflowToggled = false)}
236-
><ChevronLeft size={20} /></button
212+
><ChevronLeftIcon size={20} /></button
237213
>
238214
</div>
239215
{:else if canEdit(comment) || canDelete(comment)}
240216
<button
241217
on:click={() => (isOverflowToggled = true)}
242218
class="overflow-menu-button"
243219
title="Click '...' to see more options for this comment"
244-
><OverflowMenuHorizontal size={20} /></button
220+
><OverflowMenuIcon size={20} /></button
245221
>
246222
{/if}
247223
</div>

src/components/CommentInput.svelte

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<script lang="ts">
22
import Login from "./Login.svelte"
33
import SkeletonCommentInput from "./low-level/SkeletonCommentInput.svelte"
4+
import { AddComment as AddCommentIcon } from "carbon-icons-svelte"
5+
import { CloseOutline as CancelIcon } from "carbon-icons-svelte"
46
import type {
57
Comment,
68
CommentId,
@@ -15,13 +17,16 @@
1517
import { postComment } from "../apiClient"
1618
import { useMachine } from "@xstate/svelte"
1719
import { LoginTab } from "../lib/simple-comment-types"
20+
import { open } from "../lib/svelte-transitions"
1821
export let currentUser: User | undefined
1922
export let commentId: CommentId
2023
export let onCancel = null
2124
export let autofocus = false
2225
export let placeholder = "Your comment"
26+
export let isRoot = false
2327
2428
let commentText = ""
29+
let hasEnteredComment = false
2530
let buttonCopy = "Add comment"
2631
let loginStateValue
2732
let textareaRef
@@ -165,6 +170,22 @@
165170
textAreaHeight = `${blockSize}px`
166171
})
167172
173+
const getMaxHeight = () => {
174+
if (currentUser) return "10rem"
175+
switch (loginTabSelect) {
176+
case LoginTab.guest:
177+
return "33rem"
178+
case LoginTab.signup:
179+
return "55rem"
180+
case LoginTab.login:
181+
return "33rem"
182+
183+
default:
184+
console.error(`Unknown loginTabSelect: ${loginTabSelect}`)
185+
return "10rem"
186+
}
187+
}
188+
168189
onMount(() => {
169190
resizeObserver.observe(textareaRef)
170191
})
@@ -193,14 +214,21 @@
193214
).includes($state.value)
194215
195216
$: buttonCopy = getButtonCopy(loginTabSelect, commentText, loginStateValue)
217+
$: hasEnteredComment = !!commentText?.length
196218
</script>
197219

198220
<SkeletonCommentInput
199221
width={textAreaWidth}
200222
height={textAreaHeight}
201223
isHidden={!isProcessing}
202224
/>
203-
<form class="comment-form" class:is-hidden={isProcessing} on:submit={onSubmit}>
225+
<form
226+
class="comment-form"
227+
class:is-hidden={isProcessing}
228+
on:submit={onSubmit}
229+
in:open={{ axis: "y", duration: 250, max: getMaxHeight() }}
230+
out:open={{ axis: "y", delay: 250, duration: 125, max: getMaxHeight() }}
231+
>
204232
<!-- svelte-ignore a11y-autofocus -->
205233
<textarea
206234
class="comment-field"
@@ -211,15 +239,17 @@
211239
{placeholder}
212240
dir="auto"
213241
/>
214-
<Login {currentUser} />
215-
{#if !currentUser || (commentText && commentText.length)}
216-
<div class="button-row">
242+
<Login {currentUser} isVisible={hasEnteredComment} />
243+
{#if !currentUser || hasEnteredComment}
244+
<div class="button-row comment-footer">
245+
<button class="comment-submit-button" type="submit"
246+
><AddCommentIcon size={20} />{buttonCopy}</button
247+
>
217248
{#if onCancel !== null}
218249
<button class="comment-cancel-button" type="button" on:click={onCancel}
219-
>Cancel</button
250+
><CancelIcon size={20} />Cancel</button
220251
>
221252
{/if}
222-
<button class="comment-submit-button" type="submit">{buttonCopy}</button>
223253
</div>
224254
{/if}
225255
</form>

src/components/DiscussionDisplay.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@
186186
{#if showReply === discussionId}
187187
<CommentInput
188188
commentId={discussionId}
189+
isRoot={true}
189190
{currentUser}
190191
on:posted={onCommentPosted}
191192
/>

src/components/Login.svelte

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts">
2-
import { fly } from "svelte/transition"
2+
import { fade, fly } from "svelte/transition"
33
import type {
44
AdminSafeUser,
55
ServerResponse,
@@ -56,6 +56,7 @@
5656
const USER_ID_HELPER_TEXT = "This is the user id that uniquely identifies you"
5757
5858
export let currentUser: User | undefined
59+
export let isVisible = false
5960
6061
let self: User = currentUser
6162
let isError = false
@@ -682,7 +683,12 @@
682683
}
683684
</script>
684685

685-
<section class="simple-comment-login" class:is-loading={!isLoaded}>
686+
<section
687+
class="simple-comment-login"
688+
class:is-loading={!isLoaded}
689+
class:is-visible={isVisible}
690+
transition:fade
691+
>
686692
{#if !self}
687693
<div class="selection-tabs button-row">
688694
<button

src/components/SelfDisplay.svelte

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020
2121
$: {
2222
isProcessing =
23-
loginStateValue === "verifying" ||
24-
loginStateValue === "loggingIn" ||
25-
loginStateValue === "loggingOut"
23+
// loginStateValue === "verifying" ||
24+
loginStateValue === "loggingIn" || loginStateValue === "loggingOut"
2625
}
2726
const onLogoutClick = (e: Event) => {
2827
e.preventDefault()

src/lib/svelte-transitions.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { linear } from "svelte/easing"
2+
3+
export const open = (
4+
_element: HTMLElement,
5+
params?: {
6+
delay?: number
7+
duration?: number
8+
easing?: (t: number) => number
9+
direction?: "in" | "out" | "both"
10+
axis?: "x" | "y"
11+
max?: string
12+
}
13+
) => {
14+
const {
15+
delay = 0,
16+
duration = 250,
17+
easing = linear,
18+
axis = "x",
19+
max = "100%",
20+
} = params ?? {}
21+
22+
const [_, magnitude, unit] = max.match(/^(\d+\.?\d*)([a-zA-Z%]*)$/) ?? [
23+
undefined,
24+
"100",
25+
"%",
26+
]
27+
28+
const css = (t: number, u: number) => {
29+
const percent = (1 - u) * parseFloat(magnitude)
30+
const dimension = axis === "x" ? "width" : "height"
31+
return `max-${dimension}: ${percent}${unit}`
32+
}
33+
34+
return {
35+
delay,
36+
duration,
37+
easing,
38+
css,
39+
}
40+
}

src/scss/simple-comment-style.scss

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,10 @@ $highlight-text-color: color.scale(
301301
margin-bottom: 5rem;
302302

303303
form.comment-form {
304+
overflow: hidden;
305+
will-change: max-height;
304306
margin-bottom: 3rem;
307+
305308
.comment-update-button {
306309
width: 12.5rem;
307310
}
@@ -537,7 +540,7 @@ $highlight-text-color: color.scale(
537540
}
538541
}
539542
&.is-rtl {
540-
.comment-body {
543+
> .comment-body {
541544
text-align: right;
542545
}
543546
}

0 commit comments

Comments
 (0)