From dec9bef3821b9339e2af609e7fa0b489358a5e32 Mon Sep 17 00:00:00 2001 From: Josh de Leeuw Date: Mon, 16 Oct 2023 11:56:46 -0400 Subject: [PATCH] Add grid option, make it default --- examples/jspsych-html-button-response.html | 10 ++++- packages/jspsych/src/index.scss | 17 +++++++- .../plugin-html-button-response/src/index.ts | 39 ++++++++++++------- 3 files changed, 49 insertions(+), 17 deletions(-) diff --git a/examples/jspsych-html-button-response.html b/examples/jspsych-html-button-response.html index 6cce713788..39bdeeaf35 100644 --- a/examples/jspsych-html-button-response.html +++ b/examples/jspsych-html-button-response.html @@ -20,7 +20,15 @@ type: jsPsychHtmlButtonResponse, stimulus: '

GREEN

', choices: ['Green', 'Blue', 'Red'], - prompt: "

What color is this word?

" + prompt: "

What color is this word? (flex layout)

" + }); + + timeline.push({ + type: jsPsychHtmlButtonResponse, + stimulus: '

GREEN

', + choices: ['Green', 'Blue', 'Red'], + button_layout: "grid", + prompt: "

What color is this word? (grid layout)

" }); timeline.push({ diff --git a/packages/jspsych/src/index.scss b/packages/jspsych/src/index.scss index 5722a2b21e..4093c9824c 100644 --- a/packages/jspsych/src/index.scss +++ b/packages/jspsych/src/index.scss @@ -58,11 +58,24 @@ font-size: 14px; } +/* Buttons and Button Groups */ + +.jspsych-btn-group-flex { + display: flex; + justify-content: center; +} + +.jspsych-btn-group-grid { + display: grid; + grid-auto-columns: max-content; + min-width: fit-content; +} + /* borrowing Bootstrap style for btn elements, but combining styles a bit */ .jspsych-btn { display: inline-block; - padding: 6px 12px; - margin: 0px; + padding: 8px 12px; + margin: 0.75em; font-size: 14px; font-weight: 400; font-family: "Open Sans", "Arial", sans-serif; diff --git a/packages/plugin-html-button-response/src/index.ts b/packages/plugin-html-button-response/src/index.ts index 0c3c33a213..1ec684a3f7 100644 --- a/packages/plugin-html-button-response/src/index.ts +++ b/packages/plugin-html-button-response/src/index.ts @@ -45,17 +45,25 @@ const info = { pretty_name: "Trial duration", default: null, }, - /** The vertical margin of the button. */ - margin_vertical: { + /** The CSS layout for the buttons. Options: 'flex' or 'grid'. */ + button_layout: { type: ParameterType.STRING, - pretty_name: "Margin vertical", - default: "0px", + pretty_name: "Button layout", + default: "grid", }, - /** The horizontal margin of the button. */ - margin_horizontal: { + /** The number of grid rows when `button_layout` is "grid" */ + button_rows: { type: ParameterType.STRING, - pretty_name: "Margin horizontal", - default: "8px", + pretty_name: "Grid rows", + default: "1", + }, + /** The number of grid columns when `button_layout` is "grid". + * Using "auto" will make the number of columns equal the number + * of buttons. */ + button_cols: { + type: ParameterType.STRING, + pretty_name: "Grid columns", + default: "auto", }, /** If true, then trial will end when user responds. */ response_ends_trial: { @@ -90,12 +98,15 @@ class HtmlButtonResponsePlugin implements JsPsychPlugin { // Display buttons const buttonGroupElement = document.createElement("div"); buttonGroupElement.id = "jspsych-html-button-response-btngroup"; - buttonGroupElement.style.cssText = ` - display: flex; - justify-content: center; - gap: ${trial.margin_vertical} ${trial.margin_horizontal}; - padding: ${trial.margin_vertical} ${trial.margin_horizontal}; - `; + if (trial.button_layout === "grid") { + buttonGroupElement.classList.add("jspsych-btn-group-grid"); + let n_cols = + trial.button_cols === "auto" ? trial.choices.length : parseInt(trial.button_cols); + buttonGroupElement.style.gridTemplateColumns = `repeat(${n_cols}, 1fr)`; + buttonGroupElement.style.gridTemplateRows = `repeat(${trial.button_rows}, 1fr)`; + } else if (trial.button_layout === "flex") { + buttonGroupElement.classList.add("jspsych-btn-group-flex"); + } for (const [choiceIndex, choice] of trial.choices.entries()) { buttonGroupElement.insertAdjacentHTML("beforeend", trial.button_html(choice, choiceIndex));